Java static member initialisation [duplicate] - java

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`

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);

static variable declaration in class java [duplicate]

This question already has answers here:
Java: When is a static initialization block useful?
(13 answers)
Closed 5 years ago.
Why is this declaration wrong? This declaration leads to identifier expected error
class Abc{
static ArrayList<Integer> p;
p = new ArrayList<Integer>(); // identifier expected error
}
You have a freestanding assignment statement in your class body. You can't have step-by-step code there, it has to be within something (an initializer block, a method, a constructor, ...). In your specific case, you can:
Put that on the declaration as an initializer
static ArrayList<Integer> p = new ArrayList<>();
Wrap it in a static initialization block
static {
p = new ArrayList<Integer>();
}
More in the tutorial on initializing fields.
This is the right way to do it :
import java.util.ArrayList;
public class Abc {
static ArrayList<Integer> p;
static {
p = new ArrayList<Integer>(); // works
}
}

Static Variables in Java [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
What is the difference between referencing a field by class and calling a field by object?
(5 answers)
Closed 5 years ago.
I know that static variables are part of class and not part of the Object . How can the following lines of code work without any problem
class M
{
static int i=0;
void Inc()
{
System.out.println("Global "+M.i);
System.out.println("Local "+this.i);
}
}
public class StaticTest
{
public static void main(String args[])
{
M m1=new M();
m1.i=99; //How can the m1 object access i variable of the class
m1.Inc();
}
}
The output i get is
Global 99
Local 99
How can the m1 object access i variable of the class?
It is the very same i variable in both cases.
unfortunately, java allows you to access static fields using non-static syntax.
That is all there is to this, nothing else behind this.
Yes, it is allowed for non-static members to access and update static members.
See this for more information here

Instance variable value not being printed [duplicate]

This question already has answers here:
non static variable name cannot be referenced from a static context [duplicate]
(5 answers)
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 6 years ago.
I am trying to learn java. currently learning about types of variables.
i have written a small program defining instance,local,static variables and trying to print the same from with in the main method. but i am getting error saying "non static variable i cannot be referenced from static context. Below is my program
public class variable{
int i=5;
static int j=10;
public static void main(String[] args){
int k=15;
System.out.println(i);
System.out.println(j);
System.out.println(k);
}
}
Please let me know whats wrong with the program
You need to create a instance of variable and access i
variable v = new variable();
// then access v.i
BTW use Camelcase for you class name.
int i should be static becasue static context cant refer to the non-static variable
Options:
Make a new instance of your class so you can reach i. In fact it is maybe not the best option, because you should make it private, and add a getter method... :)
OR
You could change int i to static int i, because of the static main method.
+1 : it is better to have classnames camescased... :)

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

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

Categories