Java static instance VS get-method - java

I've been thinking about the difference between these code snippets. I understand that you can not set instance field if you are using getInstance (Second option below), but is there other differences?
public class MainClass {
public static MainClass instance;
public static void main(String[] args) {
instance = new MainClass();
}
public void HelloWorld() {
System.out.println("This is a test!");
}
}
VS
public class MainClass {
private static MainClass instance;
public static void main(String[] args) {
instance = new MainClass();
}
public MainClass getInstance() {
return instance;
}
public void HelloWorld() {
System.out.println("This is a test!");
}
}
What is the difference between using "MainClass.instance.HelloWorld();" (First) or "MainClass.getInstance().HelloWorld();" (Second)
TLDR: Which one, and why? What is the difference?
Thanks! :)

In the first example, you have declared instance as public making it vulnerable to accidental changes and therefore it is not recommended.
In the second example, you have declared instance as private making it invisible outside the class and thus ensuring that if required, it can be changed only through a public mutator/setter where you can put the desired logic how you want it to be changed.

Scalability
The difference is somewhere down the line if your program has many calls to the instance, and you want to change where the instance comes from or perform an additional action while retrieving the instance, you can modify the getInstance() method, instead of adding code in every location where you used instance.

Public
Current code is vulnerable by outsider who can change your instance with new one or even their own by subclassing. If you do not have need to change instance after first init then make it final and then public is good.
private
Saves you from above problem. Gives you more control to change instance if needed.

Related

Can we create an object of the class in which main function is defined in Java?

Is it possible to create object of class in which main method resides.I have been searching for this answer but I have been told that it depends on compiler some compiler will allow while others will not.Is that true?
Yes? The main method is just an entry point. The class is like any other, except it has an additional public static method. The main method is static and therefore not part of the instance of the object, but you shouldn't be using the main method anyway except for starting the program.
public class Scratchpad {
public static void main(String[] args) {
Scratchpad scratchpad = new Scratchpad();
scratchpad.someMethod();
}
public Scratchpad() {
}
private void someMethod() {
System.out.println("Non-static method prints");
}
}
Yes, you can create object for the class which has main method. There is no difference in this class and a class which don't have main method with respect to creating objects and using.

What is the difference between static reference variable and instance reference variable in initiating?

I'm confused about an essential thing in java.
public class InitItself1 {
public InitItself1(){}
private InitItself1 me = new InitItself1();
}
Of course I know that the StackOverFlowError will be occurred when creating an instance of the above class. The above class will be initiated recursively itself because of the initiation of the variable "me".
But,
public class InitItself2 {
public InitItself2(){}
private static InitItself2 me = new InitItself2();
}
Of course the outcome of the above class, "InitItself2" is different to the prior class, "InitItself1". This works just fine, no error occurred. As I know, initiating static variables and executing static blocks are performed when classes in which static variables and blocks are loaded.
What makes me confused is that I think it's the same that the variables, "me" of both classes, "InitItself1" and "InitItself2" are initiated, and also they have references to their classes in which they are, so it looks that "initiating recursively" would happen in initiating both classes.
What is the point that I'm missing?
Good answer please.
Thanks :)
You are not going to get StackOverFlowError in the second case. As you have said yourself, static variables are initiated when the class is loaded, and because a class is only loaded once, the static InitItself2 me will only be instantiated once. Creating a new object with constructor doesn't require the class to be reloaded.
public final class InitItself {
static {
System.out.println("Class is loaded");
}
private static InitItself me = new InitItself();
static {
System.out.println("me is instantiated");
}
public InitItself() {
System.out.println("Constructor called, me=" + me);
}
public static void main(String[] args) {
System.out.println("START");
InitItself i = new InitItself();
System.out.println("FINISH");
}
}
Gives the following output
Class is loaded
Constructor called, me=null
me is instantiated
START
Constructor called, me=oop.InitItself#6ff3c5b5
FINISH

Local variables goes missing

I face a strange error. During the startup of the server, i initialize a set of variables in the init() method of a java class. I could see this value is persisted during the server startup. However, when i try to login through the WebUI, these local variables goes missing.
However, if i assign the variable in a static block, it stays on.
I dont know how this could happen. Any pointers would help.
Java version : JRE1.7.0_40
My piece of code looks like
ClassA.java
public class ClassA { public static String testString; public static
void init() throws Exception {
testString = "testSTring222"; } }
ClassB.java
ClassA.init(); System.out.println(ClassA.testString)
Please help me get out of this.
It certainly depends on what does your server.
Use some static variable.
This could happen due to the usage of multiple class loaders, since static variables are not global across multiple class loaders. i.e. if the same class is loaded in 2 different class loaders, then you will have 2 copies of the static variable.
Class A
public class ClassA
{
public static String testString = "testSTring222";
public static void main(String[] args)
{
}
}
Class B
public class ClassB
{
static ClassA cA = new ClassA();
public static void main(String[] args)
{
System.out.println(cA.testString);
}
}
So you should reference Class A as a static class and make the and set the static string when you first declare it as a variable

How is the Static method(main) able to grab hold of non static method(constructor) and execute it?

Seems like a very basic query but I was pondering how the static method main() below is able to execute a non static method(the constructor obviously) from it using the new keyword. Though I understand that new brings onto the table a few other things as well but how should I convince myself that this isn't an exception to the rule that static and non static methods can't using non static and static context respectively?
Below is the sample code:
public class ConstructorTest {
ConstructorTest(String str)
{
System.out.println("Constructor Printing "+str);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConstructorTest cnst=new ConstructorTest("here");
}
}
The above code actually prints --> Constructor Printing here
or in other words executing the body of a Non static method from a Static method?
Any plausible explanations are welcome.
The Java Tutorial states that
[...] Constructors are not members.
Therefore, there is no problem in calling them, since they are not bound to instances of your class. This would not make sense - hence, you cannot do the following:
Thing thing = new Thing();
Thing anotherThing = thing.Thing();
A constructor is not a method, so you cannot apply "method logic" to them.
In case you want to know more, the whole instantiation process is very well documented in the JLS. See 12.5. Creation of New Class Instances.
Actually constructor is compiled into the static method, this is how JVM internally creates instances of classes.
You are executing non-static code, but you are not doing it in a static context.
for instance:
public class C1{
private int x;
public String do(){ System.out.println("x = " + x);}
public static void main(String[] args){
do();
}
}
This can not work, since do is an instance method, which might run code that is specific to the instance. So, how would the VM know which instance to use, or what value x should have?
Now, to first use a constructor, which is possible from any context:
public class C1{
private int x;
public String do(){ System.out.println("x = " + x);}
public static void main(String[] args){
C1 t = new C1();
t.do();
}
}
Here, even though you are calling the method from within a static method, you are using it through an instance, so not in a static context.
ConstructorTest is not a method.
its an constructor,and you can use the constructor for initialize class property.
you can also initialize the static variable from the constructor like that :-
public class XYZ
{
static int i=0;
public XYZ() {
i=1;//not an compile time error
}
public static void doSome(){}
public static void main(String[] args) {
}
}
On a formal language level you should read the line
ConstructorTest cnst = new ConstructorTest("here")
as a class instance creation expression. As a matter of fact, this is not a call to a constructor or any other method.
The instance creation does many steps, like allocating memory for the new object, initializing the fields, calling constructors and initializer blocks. See JLS §12.5 for a detailed step-by-step description. Thus being said, the constructor invocation is only a part of the instance creation.
Additionally, you might see constructors as being static parts of the class. In fact, constructor declaration are not members (see JLS §8.8) and thus they are not overridable (as static methods also). Beware: This is only half true. When being inside the constructor you already have the instance created, and you are able to call other instance methods and/or access instance fields.

Invoke static initializer again

Once a class is loaded is there a way to invoke static initializers again?
public class Foo {
static {
System.out.println("bar");
}
}
Edit:
I need to invoke the static initializer because I didn't write the original class and the logic I need to invoke is implemented in the static initializer.
Put the initalisation code in a separate public static method, so you can call it from the static initializer and from elsewhere?
One circumstance in which the logic would be run more than once is if the class is loaded multiple times by different ClassLoaders. Note that in this instance, they are essentially different classes.
Generally, though, these are one-shot deals. If you want to be able to invoke the logic multiple times, do as others have suggested and put it in a static method.
I agree with Earwicker's answer. Just extract the static initialization to a separate static method.
public class Foo {
static {
Foo.initialize();
}
public static void initialize() {
System.out.println("bar");
}
}
In case you really want the exact answer to your exact question, the answer is no. It's not possible to invoke a static initializer or an instanceInitializer via reflection.
The docs clearly says :
for getDeclaredMethod(String name) :
If the name is "<init>" or "<clinit>" a NoSuchMethodException is raised.
for getDeclaredMethods() :
The class initialization method is not included in the returned array.
So no, it's not possible to invoke it, even via reflection.
you could try extending the class which contains the static code, and then put in your own static initializer. Not quite sure if it works, but :
public class OldBadLibraryClass {
static {
System.out.println("oldBadLibrary static init");
}
}
//next file
public class MyBetterClass extends OldBadLibraryClass {
static {
System.out.println("MyBetterClass init");
}
}
public class Test {
public static void main(String[] args) {
new MyBetterClass();
}
}
see if the above prints in the order you expect. On my machine, it worked.
Though this is totally a hack, and is quite brittle. It would really be much better to modify the old class to have an init() method that can be overridden.
Here https://stackoverflow.com/a/19302726/2300018 is a post from me, where I re-load a utility class to re-run the static initializer for unit testing.

Categories