Take a look at below code: As you see while trying to access static field x via an uninitialized local variable Foo foo; code foo.x generates compilation error: Variable ‘foo’ might not have been initialized. It could seem like this error makes sense, but only until we realize that to access a static member the JVM doesn’t actually use the
Tag: static
Why in Android documentation Activity classes have static modifier
If we open android documentation we will see that Activity classes have static modifier: I’m used to declaring Activities without static modifier: What is the reason behind using static modifier in Activity class declaration? UPDATE: The static modifiers have been removed from the documentation. Answer It’s Documentation bug. Sadly, they are not rare. You’d normally only use the static modifier
Are values returned by static method are static?
Please consider this code Will this static method return a new instance of MyClass every time it is called? or it is going to return a reference to the same instance over and over? Answer Lol lol lol Declaring a method static means it is a class method and can be called on the class without an instance (and can’t
multiple constructors for final fields in java
I have some final fields in the class like But this gives an error that final field ‘b’ might not have been initialized. So any help would be appreciated on how to handle final attributes initialization in case of multiple constructors. It works fine if I have only the second constructor. Answer You can initialize b to default false. All
Are static variables inherited
I have read at 1000’s of locations that Static variables are not inherited. But then how this code works fine? Parent.java Child.java This code prints “Parent”. Also read at few locations concept of data hiding. Parent.java Child.java Now the output is “Child”. So does this mean that static variables are inherited but they follow the concept of data-hiding? Answer Please
Synchronization with static block
I have a question: can we use the static keyword with a synchronized method? As we all know that static is related to class and synchronization is used to block an object, using synchronized with static doesn’t make any sense to me. So why and in which situation would I use synchronization with the static keyword? Answer I think this
Mocking static methods with Mockito
I’ve written a factory to produce java.sql.Connection objects: I’d like to validate the parameters passed to DriverManager.getConnection, but I don’t know how to mock a static method. I’m using JUnit 4 and Mockito for my test cases. Is there a good way to mock/verify this specific use-case? Answer Use PowerMockito on top of Mockito. Example code: More information: Why doesn’t
How to keep a variable value even after doing run as-> Java Application?
I know that when we use static variables the value remains the same for all the instances of a class. But when I do run as -> Java Application in a class, even the static variable is reinicialized with the default value. Is there any way to keep a variable value even after doing run as-> Java Application in a
Is it bad practice to keep data in static variables?
In an Android application, is it bad practice to store objects in static fields in these cases? Application data. Is it bad to keep application data in static variables in a class while the application is running? Currently, I’m storing the data in an instance variable in my Application class. Then classes that need the data can obtain the data
Shall we mark private methods as “static” as more as possible?
There are some private methods in a class, and they don’t depend on any instance variables. They accept some arguments, and return a value, no side effects. For example: You see there is a capWord method in this class, which is private but not static. It’s safe to mark it as static, but is there good enough reason to do