when is static variable loaded in java, runtime or compile time? - java

When is static variable loaded, runtime or compile time? Can someone please explain it.
I would really appreciate the help.
Thank you.

The compiler optimizes inlineable static final fields by embedding the value in the bytecode instead of computing the value at runtime.
When you fire up a JVM and load a class for the first time (this is done by the classloader when the class is first referenced in any way) any static blocks or fields are 'loaded' into the JVM and become accessible.
A demonstration:
public class StaticDemo {
// a static initialization block, executed once when the class is loaded
static {
System.out.println("Class StaticDemo loading...");
}
// a constant
static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
// a static field
static int instanceCounter;
// a second static initialization block
// static members are processed in the order they appear in the class
static {
// we can now acces the static fields initialized above
System.out.println("ONE_DAY_IN_MILLIS=" + ONE_DAY_IN_MILLIS
+ " instanceCounter=" + instanceCounter);
}
// an instance initialization block
// instance blocks are executed each time a class instance is created,
// after the parent constructor, but before any own constructors (as remarked by Ahmed Hegazy)
{
StaticDemo.instanceCounter++;
System.out.println("instanceCounter=" + instanceCounter);
}
public static void main(String[] args) {
System.out.println("Starting StaticDemo");
new StaticDemo();
new StaticDemo();
new StaticDemo();
}
static {
System.out.println("Class StaticDemo loaded");
}
}
Output:
Class StaticDemo loading...
ONE_DAY_IN_MILLIS=86400000 instanceCounter=0
Class StaticDemo loaded
Starting StaticDemo
instanceCounter=1
instanceCounter=2
instanceCounter=3
Notice how 'Starting StaticDemo' does not appear as the first line of output. This is because the class must be loaded before the main method can be executed, which means all static fields and blocks are processed in order.

They are loaded at runtime.
Static means: that the variable belong to the class, and not to instances of the class. So there is only one value of each static variable, and not n values if you have n instances of the class.

run time when class is loaded.
- Have a look at initialization

The static fields are loaded when the class is loaded. This usually happens which the file object of a class is created, but it can be earlier if the class is used another way.
The static initialiser is thread safe and you can access the class in multiple threads safely. This is useful as a way to create a thread safe singleton without having to use a lock.
Note: the class can be loaded (and its static intialisation block run) more than once if multiple class loaders are used. Generally, loading the same class in multiple class loaders can be confusing and is avoided, but it is supported and does work.

How would you load a variable at compile time? The variable is initialized when the corresponding class is loaded. See the JVMS.

Loading is a runtime operation. Everything is loaded at runtime.

When you type java ClassName then class loads into JVM with static variables, so you don't need an object for it.
Where as instance variable loaded by JVM when the object is created.

Static fields gets loaded at the time of class loading in JVM.

Related

Prevent a class from loading until a certain class is loaded

The title says it all. I'm working on top of another project (aka working with Forge) and I want to put a certain class (class no. 1) on hold - not load it until another class (class no. 2) has been loaded. Then after that certain class (class no.2) has been loaded, the first class (class no. 1) should be normally loaded.
You can load your Class1 when Class2 gets loaded.
Class1 {
public static void loadMe() {}
}
class2 {
public Class2() {
// Calling a dummy static member for loading the class
Class1.loadMe();
}
}
The advantage is that both will be loaded by the same ClassLoader by default. Alternatively, you can,
Class.forName("Class1");
In both the cases, no one in the entire App should try to access any static member of this class or it will get loaded.

Static variable fields not visible in debbuger

Hi I've got a very simple class defined like this
public class Pokus {
public static String loginToken;
public String neco = "neco";
public Pokus() {
}
public static String getLoginToken() {
return loginToken;
}
public static void setLoginToken(String loginToken) {
Pokus.loginToken = loginToken;
}
}
When I create an instance of this class
Pokus pokus = new Pokus();
pokus.setLoginToken("bla1212");
In a debugger I can see that object pokus has a field/variable called "neco" but not that static variable "loginToken".
debbuger in Android Studio
Is there any way to see static variables as well as the non-static ones?
Thanks guys I knew all of this but didn't know that debugger is taking this into consideration. There is an option to show static field Settings > Build,Execution, Deployement > Debugger > Data Views > Java
Debugger shows it properly, pokus is instance of class Pokus so it have standard method and properties from class Pokus, static methods and properties are in Class not in instance of Class. Static properties are shared for every object created from class Pokus ( or for every component in program if their are public ) so debugger properly not shows them as properties of single instance.
To show static variable examine class not instance. When debugger stops on breakpoint You can use console and write Pokus.someStaticVar and You will see current value. Console is available in debugger - http://imgur.com/a/nHfEo.
You can right-click in the debugger's Variables area and select Customize Data Views...
In there, you can choose to add static and final static fields.
Static variables have the same values for all the instance of the class.Moreover they should be accessed using class and not an instance of the class. In Java, when you declare something as static, you are saying that it is a member of the class, not the object (hence why there is only one). Therefore it doesn't make sense to access it on the object, because that particular data member is associated with the class.
That is the reason i think debugger does not show and it is correct behaviour

NoClassDefFoundError when adding classes to the classpath at runtime when I used abstract or interface in Main class

I know Java loads Classes in first Access (creating new Instance, calling static method or static field), but in this simple example I try to execute a jar file that uses some classes which there aren't in my ClassPath at run time. I expect (because of loading classes in first access) print my messages in static block and main method before an exception occurred. but I got "Exception in thread "main" java.lang.NoClassDefFoundError: com/example/DateAbstract" and nothing printed.
This occurred when I used an abstract class or interface in main class which that classes or interfaces are in another jar file.
public class Driver {
static { System.out.println("I am first.[static block]"); }
public static void main(String[] args) {
System.out.println("I am first.[ main method]");
DateAbstract date = new CustomDate();
System.out.println(date.sayDate());
}
in my another jar :
public class CustomDate extends DateAbstract {
#Override
public String sayDate() {
return new Date().toString();
}
public abstract class DateAbstract {
public abstract String sayDate();
}
when I use this code for add my classes to classpath at runtime. nothing changed. I got execption before execute static block.
public class Driver {
static {
System.out.println("I am first.[static block]");
try {
URL url = new File("lib/DateApi.jar").toURI().toURL();
URLClassLoader urlClassLoader = (URLClassLoader) URLClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(urlClassLoader,url);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("I am first.[ main method]");
DateAbstract date = new CustomDate();
System.out.println(date.sayDate());
}
}
Questions :
why is this happening and how to solve it ?
It’s not correct to say that in Java classes are loaded on their first access. You are confusing this with the initialization of a class, which implies executing the Java code of static initializer blocks and field initializers. The loading and verification might happen at an earlier time; the specification provides some freedom to the JVMs in this regard.
The key point here is that your main method instantiates an object of type CustomDate, stores it into a variable of the compile-time type DateAbstract and then tries to invoke sayDate() on that variable. This combination of instantiating CustomDate and invoking DateAbstract.sayDate() on it requires the verification of its correctness, i.e. whether CustomDate is a subtype DateAbstract. So the loading of these two classes will already happen at verification time.
You can easily check that this is the cause. If you change the type of the local variable date to CustomDate, the instantiated type and the receiver type of the method invocation are the same, so the correctness can be proven without loading the type, so it will be indeed deferred to the actual attempt to instantiate CustomDate, hence the messages will be printed.
Still, the loading time is an implementation-specific detail. A different JVM could load the referenced classes eagerly, even if they are not required for verification. The only safe way to ensure a deferred loading, is to use dynamic loading, e.g. Class.forName(String). Note that within the class detached this way, all types might be again referenced ordinarily. So if you do the dynamic loading once after the class path has been adjusted, there is not much impact on how you have to write the code nor its performance. Of course, having the code adjusting the class path and the code depending on it within the same class won’t work reliably.

java static class / variables

Unsure about static variables.
import java.io.File;
public class Logger {
public static final File log = new File(File.listRoots()[0], "log.log");
public static void log (String message) {
/* ... */
}
}
Is the variable log pointing to the same memory throughout the life of the program? Basically is the log definition new File(File.listRoots()[0], "log.log") calculated multiple times or just one, and when?
Thanks in advance.
It is invoked once per classloader. Which, normally, means once.
A static variable is initialized as soon as the class declaring it is loaded by the classloader, and stays there until the classloader is destroyed, which in most cases means - at the end of the program execution / application lifecycle.
Just once. AFAIK, when class is loaded.

What is the difference between "Class.forName()" and "Class.forName().newInstance()"?

What is the difference between Class.forName() and Class.forName().newInstance()?
I do not understand the significant difference (I have read something about them!). Could you please help me?
Maybe an example demonstrating how both methods are used will help you to understand things better. So, consider the following class:
package test;
public class Demo {
public Demo() {
System.out.println("Hi!");
}
public static void main(String[] args) throws Exception {
Class clazz = Class.forName("test.Demo");
Demo demo = (Demo) clazz.newInstance();
}
}
As explained in its javadoc, calling Class.forName(String) returns the Class object associated with the class or interface with the given string name i.e. it returns test.Demo.class which is affected to the clazz variable of type Class.
Then, calling clazz.newInstance() creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. In other words, this is here actually equivalent to a new Demo() and returns a new instance of Demo.
And running this Demo class thus prints the following output:
Hi!
The big difference with the traditional new is that newInstance allows to instantiate a class that you don't know until runtime, making your code more dynamic.
A typical example is the JDBC API which loads, at runtime, the exact driver required to perform the work. EJBs containers, Servlet containers are other good examples: they use dynamic runtime loading to load and create components they don't know anything before the runtime.
Actually, if you want to go further, have a look at Ted Neward paper Understanding Class.forName() that I was paraphrasing in the paragraph just above.
EDIT (answering a question from the OP posted as comment): The case of JDBC drivers is a bit special. As explained in the DriverManager chapter of Getting Started with the JDBC API:
(...) A Driver class is loaded, and
therefore automatically registered
with the DriverManager, in one of two
ways:
by calling the method Class.forName. This explicitly loads
the driver class. Since it does not
depend on any external setup, this way
of loading a driver is the recommended
one for using the DriverManager
framework. The following code loads
the class acme.db.Driver:
Class.forName("acme.db.Driver");
If acme.db.Driver has been written so that loading it causes an
instance to be created and also calls
DriverManager.registerDriver with that
instance as the parameter (as it
should do), then it is in the
DriverManager's list of drivers and
available for creating a connection.
(...)
In both of these cases, it is the responsibility of the newly-loaded Driver class to register itself by calling DriverManager.registerDriver. As mentioned, this should be done automatically when the class is loaded.
To register themselves during initialization, JDBC driver typically use a static initialization block like this:
package acme.db;
public class Driver {
static {
java.sql.DriverManager.registerDriver(new Driver());
}
...
}
Calling Class.forName("acme.db.Driver") causes the initialization of the acme.db.Driver class and thus the execution of the static initialization block. And Class.forName("acme.db.Driver") will indeed "create" an instance but this is just a consequence of how (good) JDBC Driver are implemented.
As a side note, I'd mention that all this is not required anymore with JDBC 4.0(added as a default package since Java 7) and the new auto-loading feature of JDBC 4.0 drivers. See JDBC 4.0 enhancements in Java SE 6.
Class.forName() gives you the class object, which is useful for reflection. The methods that this object has are defined by Java, not by the programmer writing the class. They are the same for every class. Calling newInstance() on that gives you an instance of that class (i.e. calling Class.forName("ExampleClass").newInstance() it is equivalent to calling new ExampleClass()), on which you can call the methods that the class defines, access the visible fields etc.
In JDBC world, the normal practice (according the JDBC API) is that you use Class#forName() to load a JDBC driver. The JDBC driver should namely register itself in DriverManager inside a static block:
package com.dbvendor.jdbc;
import java.sql.Driver;
import java.sql.DriverManager;
public class MyDriver implements Driver {
static {
DriverManager.registerDriver(new MyDriver());
}
public MyDriver() {
//
}
}
Invoking Class#forName() will execute all static initializers. This way the DriverManager can find the associated driver among the registered drivers by connection URL during getConnection() which roughly look like follows:
public static Connection getConnection(String url) throws SQLException {
for (Driver driver : registeredDrivers) {
if (driver.acceptsURL(url)) {
return driver.connect(url);
}
}
throw new SQLException("No suitable driver");
}
But there were also buggy JDBC drivers, starting with the org.gjt.mm.mysql.Driver as well known example, which incorrectly registers itself inside the Constructor instead of a static block:
package com.dbvendor.jdbc;
import java.sql.Driver;
import java.sql.DriverManager;
public class BadDriver implements Driver {
public BadDriver() {
DriverManager.registerDriver(this);
}
}
The only way to get it to work dynamically is to call newInstance() afterwards! Otherwise you will face at first sight unexplainable "SQLException: no suitable driver". Once again, this is a bug in the JDBC driver, not in your own code. Nowadays, no one JDBC driver should contain this bug. So you can (and should) leave the newInstance() away.
1 : if you are interested only in the static block of the class , the loading the class only would do , and would execute static blocks then all you need is:
Class.forName("Somthing");
2 : if you are interested in loading the class , execute its static blocks and also want to access its its non static part , then you need an instance and then you need:
Class.forName("Somthing").newInstance();
Class.forName() gets a reference to a Class, Class.forName().newInstance() tries to use the no-arg constructor for the Class to return a new instance.
"Class.forName()" returns the Class-Type for the given name. "newInstance()" does return an instance of this class.
On the type you can't call directly any instance methods but can only use reflection for the class. If you want to work with an object of the class you have to create an instance of it (same as calling "new MyClass()").
Example for "Class.forName()"
Class myClass = Class.forName("test.MyClass");
System.out.println("Number of public methods: " + myClass.getMethods().length);
Example for "Class.forName().newInstance()"
MyClass myClass = (MyClass) Class.forName("test.MyClass").newInstance();
System.out.println("String representation of MyClass instance: " + myClass.toString());
just adding to above answers, when we have a static code (ie code block is instance independent) that needs to be present in memory, we can have the class returned so we'll use Class.forname("someName") else if we dont have static code we can go for Class.forname().newInstance("someName") as it will load object level code blocks(non static) to memory
No matter how many times you call Class.forName() method, Only once the static block gets executed not multiple time:
package forNameMethodDemo;
public class MainClass {
public static void main(String[] args) throws Exception {
Class.forName("forNameMethodDemo.DemoClass");
Class.forName("forNameMethodDemo.DemoClass");
Class.forName("forNameMethodDemo.DemoClass");
DemoClass demoClass = (DemoClass)Class.forName("forNameMethodDemo.DemoClass").newInstance();
}
}
public class DemoClass {
static {
System.out.println("in Static block");
}
{
System.out.println("in Instance block");
}
}
output will be:
in Static block
in Instance block
This in Static block statement is printed only once not three times.
Class.forName()-->forName() is the static method of Class class it returns Class class object used for reflection not user class object so you can only call Class class methods on it like getMethods(), getConstructors() etc.
If you care about only running static block of your(Runtime given) class and only getting information of methods,constructors,Modifier etc of your class you can do with this object which you get using Class.forName()
But if you want to access or call your class method (class which you have given at runtime) then you need to have its object so newInstance method of Class class do it for you.It create new instance of the class and return it to you .You just need to type-cast it to your class.
ex-: suppose Employee is your class then
Class a=Class.forName(args[0]);
//args[0]=cmd line argument to give class at runtime.
Employee ob1=a.newInstance();
a.newInstance() is similar to creating object using new Employee().
now you can access all your class visible fields and methods.

Categories