Java boolean static error - java

The code gives a boolean error when I try to run it. It says boolean can not be adressed with static. What could be the answer.?
package csd;
class Uti {
public static void main(String[] args) {
boolean result;
result = Sample.foo() && Sample.bar();
System.out.printf("result%b%n",result);
}
class Sample {
public static boolean foo() {
System.out.println("foo");
return true;
}
public static boolean bar() {
System.out.println("bar");
return false;
}
}
}
Error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method foo cannot be declared static; static methods can only be declared in a static or top level type

Making your Sample class static will resolve your error:
class Uti {
public static void main(String[] args)
{
boolean result;
result = Sample.foo() && Sample.bar();
System.out.printf("result%b%n",result);
}
static class Sample {
public static boolean foo() {
System.out.println("foo");
return true;
}
public static boolean bar()
{
System.out.println("bar");
return false;
}
}
}
Making it a top level class will also work:
class Sample {
public static boolean foo() {
System.out.println("foo");
return true;
}
public static boolean bar()
{
System.out.println("bar");
return false;
}
}
class Uti {
public static void main(String[] args)
{
boolean result;
result = Sample.foo() && Sample.bar();
System.out.printf("result%b%n",result);
}
}

Well there is a compilation error in your code, you can fix your problem by adding a static modifier to parent type like this in your code:
public static void main(String[] args)
{
boolean result;
result = Sample.foo() && Sample.bar();
System.out.printf("result%b%n",result);
}
static class Sample {
public static boolean foo() {
System.out.println("foo");
return true;
}
public static boolean bar()
{
System.out.println("bar");
return false;
}
}

Related

#ExtendWith getting executed in every test class, rather than only once

I am trying to create extensions A_Extension and B_Extension, which will get executed only once at start and at the end of all test classes.
Where as, I want to call C_Extension always in each test class.
Here are my extensions and test classes.
public class A_Extension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private static boolean isInitialized = false;
#Override
public void beforeAll(ExtensionContext context) throws Exception {
if (!isInitialized) {
//Do something and return ABC
isInitialized = true;
context.getRoot().getStore(GLOBAL).put("ABC", ABC);
}
}
#Override
public void close() throws Throwable {
ABC = null;
}
}
public class B_Extension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private static boolean isInitialized = false;
#Override
public void beforeAll(ExtensionContext context) throws Exception {
if (!isInitialized) {
//Do something and return PQR
isInitialized = true;
context.getRoot().getStore(GLOBAL).put("PQR", PQR);
}
}
#Override
public void close() throws Throwable {
PQR = null;
}
}
public class C_Extension implements AfterEachCallback, AfterAllCallback, BeforeAllCallback {
//some extension code
}
#ExtendWith({A_Extension.class,B_Extension.class})
class TestClass1 {
#RegisterExtension
static C_Extension cExtension = new C_Extension();
#BeforeAll
static void beforeAllClass() { }
#AfterAll
static void afterAllClass() { }
#Test
void case1() { }
#Test
void case2() { }
}
#ExtendWith({A_Extension.class,B_Extension.class})
class TestClass2 {
#RegisterExtension
static C_Extension cExtension = new C_Extension();
#BeforeAll
static void beforeAllClass() { }
#AfterAll
static void afterAllClass() {}
#Test
void case1() {}
#Test
void case2() {}
}
But A_Extension.class and B_Extension.class are getting called always in each test case.
What can be the reason behind this?

org.awaitility.Awaitility.await() is never triggered

I saw this post on how to use org.awaitility.await()
I have this code:
public void bar() {
await().forever().pollInterval(5, SECONDS).until(foo());
}
private Callable<Boolean> foo() {
String a= "1";
return new Callable<Boolean>() {
public Boolean call() throws Exception {
return true;
}
};
}
but when I debug it, i stop with breakpoint on
await().forever().pollInterval(5, SECONDS).until(foo());
but my breakpint on String a= "1"; and return true; never stops.
Am i missing something?
I have also tried this, with no print to the screen:
public ConfigActivityLog pollForVersionAwaitingJenkins() {
ConfigActivityLog[] dbConfigChangeWrapper = new ConfigActivityLog[1];
await().atLeast(1, MINUTES).pollInterval(5, SECONDS).until(foo());
return dbConfigChangeWrapper[0];
}
private Callable<Boolean> foo() {
System.out.println("in foo1");
return new Callable<Boolean>() {
public Boolean call() throws Exception {
System.out.println("in foo2");
return true;
}
};
}

Nesting calls to static methods

I want to do something like below, but it does not work. My objective is to be able to nest function calls to static helper classes to get more brevity.
public class StaticHelper {
public static Class<StaticHelper> doSomthing() {
System.out.println("I just did something !!");
return StaticHelper.class;
}
public static Class<StaticHelper> doSomthingElse() {
System.out.println("I just did something else !!");
return StaticHelper.class;
}
public static void main(String[] args) {
// Does not compiles
StaticHelper.doSomthing().doSomthingElse();
}
}
Is this possible? If so a simple example as above will be very helpful.
I guess you want something like this.
public class StaticHelper {
private final static StaticHelper INSTANCE = new StaticHelper();
public static StaticHelper doSomthing(){
System.out.println("I just did something !!");
return INSTANCE;
}
public static StaticHelper doSomthingElse(){
System.out.println("I just did something else !!");
return INSTANCE;
}
public static void main(String[] args) {
StaticHelper.doSomthing().doSomthingElse();
}
}
or another way
public class StaticHelper {
public static SomeClass doSomthing(){
return new SomeClass().doSomthing();
}
public static SomeClass doSomthingElse(){
return new SomeClass().doSomthingElse();
}
public static void main(String[] args) {
StaticHelper.doSomthing().doSomthingElse();
}
private static class SomeClass {
public SomeClass doSomthing(){
System.out.println("I just did something !!");
return this;
}
public SomeClass doSomthingElse(){
System.out.println("I just did something else !!");
return this;
}
}
}
That is not possible, but using static imports is.
public class StaticHelper {
public static void doSomething() {
System.out.println("I just did something !!");
}
public static void doSomethingElse() {
System.out.println("I just did something else !!");
}
}
in another class:
import static StaticHelper.*;
class Other {
public static void main(String[] args) {
doSomething(); // calls static methods from StaticHelper
doSomethingElse();
}
}
or - if the methods are logically connected - you can have one static factory method and the rest are instance methods:
public class StaticHelper {
public static void beginDoingSomething() {
// static factory method - you can pass parameters to it if needed
System.out.println("I just did something !!");
return new StaticHelper();
// if needed, initialize the instance with the parameters
}
public StaticHelper andDoSomethingElse() {
// instance method
// can use the instance parameters (passed to the constructor in the static factory method)
// or use parameters passed to this method
System.out.println("I just did something else !!");
return this;
// returns this for chaining
}
}
in another class:
import static StaticHelper.*;
class Other {
public static void main(String[] args) {
doSomething().andDoSomethingElse().andDoSomethingElse();
}
}
If you name the methods nicely, you can form a sentence:
validate(object).checkEmail().checkName().checkTelephoneStartsWith("+11");
where validate(object) is a static factory method constructing a new validator instance for the given object.

Java - assert does not seem to be executed

I do not understand why the output of this code is -10:
public class Prova {
public void sip(int i){
assert i>=0 : err();
System.out.println(i);
}
public int err(){
...
}
public static void main(String[] args) {
Prova t = new Prova();
t.sip(-10);
}
}
I thought assert would launch err();

Elegant way to Extend Enums

I have a requirement that is close to extending enums and since that is not possible, after doing some research online, I came up with this approach of using interfaces and making the enums extend them.
My problem is that I have a few basic types A,B and a flag for each type that says if that has to be checked. Similarly I have some extended types C... which do the same stuff after checking their flags.
Here is the code that does this
Type Interface:
public interface Type {
public String name();
}
Here is the class that uses the basic types
public class BasicChecker {
private static boolean checkA = false;
private static boolean checkB = false;
public enum BasicType implements Type {
A, B;
}
public static boolean isCheckA() {
return checkA;
}
public static void setCheckA(boolean checkA) {
BasicChecker.checkA = checkA;
}
public static boolean isCheckB() {
return checkB;
}
public static void setCheckB(boolean checkB) {
BasicChecker.checkB = checkB;
}
public static void doStuff(String message, Type type) {
if (type.name().equalsIgnoreCase(BasicType.A.name())) {
doStuff(message, isCheckA());
} else if (type.name().equalsIgnoreCase(BasicType.B.name())) {
doStuff(message, isCheckB());
}
}
protected static void doStuff(String message, boolean flag) {
if (someCheckMethod() && flag) {
doStuff(message, flag);
}
}
private static boolean someCheckMethod() {
return false;
}
}
And this is the class that uses extended types
public class ExtendedChecker extends BasicChecker {
private static boolean checkC = false;
public enum ExtendedType implements Type {
C;
}
public static boolean isCheckC() {
return checkC;
}
public static void setCheckC(boolean checkC) {
ExtendedChecker.checkC = checkC;
}
public static void doStuff(String message, Type type) {
BasicChecker.doStuff(message, type);
if (type.name().equalsIgnoreCase(ExtendedType.C.name())) {
doStuff(message, isCheckC());
}
}
}
What I am trying to solve now is to remove all the if else cases from log method. I am also trying to see if there is a better way to do this. Please ignore the statics. I do want them to be static fields and methods.
I'm having trouble understanding exactly what you're trying to do from your description, but you may find abstract methods in enums to be useful.
For example, you could add an abstract method "foo" to your enums:
public enum BasicType implements Type {
A {
public void foo(String message) {
// Do special A stuff
}
}, B {
public void foo(String message) {
// Do special B stuff
}
};
public abstract void foo(String message);
}
And you could then use that method like this:
public static void doStuff(String message, Type type) {
type.foo(message);
}
Naturally, you could put any such abstract methods in an interface you extend, if that's useful.
public class BasicChecker {
private static final Set<Type> _doCheck = Collections.newSetFromMap(new ConcurrentHashMap<Type,Boolean>());
public enum BasicType implements Type {
A, B;
}
public static boolean isCheck(Type type) {
return return _doCheck.contains(type);
}
public static void setCheck(Type type, boolean check) {
if(check) {
_doCheck.add(type);
} else {
_doCheck.remove(type);
}
}
public static void doStuff(String message, Type type) {
doStuff(message, isCheck(type));
}
}

Categories

Resources