Skip to content
Advertisement

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

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

Advertisement