How to define a constant creator method in Java [duplicate] - java

This question already has answers here:
How to declare a constant in Java?
(3 answers)
Closed 7 years ago.
How can I define a constant in my class that returns a object? What I'm looking for is something like BigInteger.ZERO

This is how BigInteger.ZERO is defined :
public static final BigInteger ZERO = new BigInteger(new int[0], 0);

public class SomeClass {
public static final type CONST_NAME = new SomeObject();
...
}
Then you can do SomeClass.CONST_NAME

Related

"Which variable(instance or local) is in action in the public method of foo class?" [duplicate]

This question already has answers here:
What is variable shadowing used for in a Java class?
(5 answers)
Is "this" shadowing a good idea?
(10 answers)
Closed 3 years ago.
It seems like its using local variable of the method!!!! I thought it would give an error on call non static variable on static method but it did not.
public class foo{
int x = 12;
public static void go(final int x){
System.out.println(x);
}
}
Actually it doesn't have a error which is interesting..
System.out.println(x)
will print the parameter of the method go
If you want to access field of the class, use this keyworld (if your method not static):
System.out.println(this.x)
In your case, you need to have an instance of foo class and use
foo f = new foo();
System.out.println(f.x);

Java static member initialisation [duplicate]

This question already has answers here:
Java: When is a static initialization block useful?
(13 answers)
Static Initialization Blocks
(14 answers)
Closed 4 years ago.
Is there any difference in when static class member initialisation is done between the following two situations:
(1)
static ArrayList<String> x = new ArrayList<String>();
(2)
static ArrayList<String> x;
static
{
x = new ArrayList<String>();
}
As far as I understand it these are effectively equivalent and both guarantee that x is initialised once and once only, and before any class method or constructor can modify it.
First approach is less error prone, for example you can have a static block calling x.get(0); which will produce NullPointerException`

Access to the outside value from Java Lambda Expression [duplicate]

This question already has answers here:
Variable used in lambda expression should be final or effectively final
(9 answers)
Closed 4 years ago.
I have the sample code below
String value="xyz";
dataList.stream().filter(obj -> obj.equals(value))
My question is: how to have the value available in my lambda expression in Filter.
It is directly accessible if you are using Java 8, see the below code.
public static void main(String args[]) {
String value="xyz";
List<String> dataList = new ArrayList<>();
dataList.add("abc");
dataList.add("def");
dataList.add("ghi");
dataList.add("xyz");
dataList.add("jkl");
dataList.add("mno");
dataList.stream().filter(obj -> obj.equals(value)).forEach(System.out::println);
}

Java, how to convert a string to class and call its static method? [duplicate]

This question already has answers here:
Invoking a static method using reflection
(5 answers)
Closed 6 years ago.
class Item
{
public static test()
{
}
}
String s = "package.Item";
Item o = (Item)Class.forName(s).cast(Item.class); // *
o.test();
but the marked line fails:
java.lang.ClassCastException: Cannot cast java.lang.Class to items.Item
To create new instance you need to do the following
Class c = Class.forName("Item");
Item i = (Item)c.newInstance();
If you want to invoke static method you just call it on class instead of instance
Item.test();
Or you can use reflection without directly reference to class
Class c = Class.forName("Item");
Method method = c.getMethod("test");
method.invoke(null);

what is the difference? with static and without [duplicate]

This question already has answers here:
Static enum vs. Non-static enum [duplicate]
(4 answers)
Closed 7 years ago.
public class TypeMessage {
public static enum GRAPH_ERROR {
ERROR_INPUT, ERROR_GRAPH
}
public enum INPUT_TYPE {
INTEGER, DOUBLE
}
}
First enum is static, second is not static. I use TypeMessage.GRAPH_ERROR and TypeMessage.INPUT_TYPE.
Q: Does the fact that I wrote the word static in this situation has no effect?
From Java Language Specification 8.9:
A nested enum type is implicitly static.
so there is no difference between them.
For enums it doesn't matter. You can access both enums in the same way: TypeMessage.GAPH_ERROR and Typemessage.INPUT_TYPE.

Categories