13/05/2008
Java Static Variables
- Java instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration.
- Any java object that belongs to that class can modify its static variables.
- Also, an instance is not a must to modify the static variable and it can be accessed using the java class directly.
- Static variables can be accessed by java instance methods also.
- When the value of a constant is known at compile time it is declared ‘final’ using the ‘static’ keyword.
Java Static Methods
- Similar to static variables, java static methods are also common to classes and not tied to a java instance.
- Good practice in java is that, static methods should be invoked with using the class name though it can be invoked using an object. ClassName.methodName(arguments) or objectName.methodName(arguments)
- General use for java static methods is to access static fields.
- Static methods can be accessed by java instance methods.
- Java static methods cannot access instance variables or instance methods directly.
- Java static methods cannot use the ‘this’ keyword.
Java Static Classes
- For java classes, only an inner class can be declared using the static modifier.
- For java a static inner class it does not mean that, all their members are static. These are called nested static classes in java.














[...] coming to the static import part. Like the above ugly line we have been unknowingly using (abusing) the java static [...]
What is a static import i&hellip on November 17th, 2009 11:34 pmGood, concise answer.
But, I would want one more thing to be added to it:
Static methods can’t be overridden, which implies they are final methods. Rather, the concept of overridden itself should never arise because static methods are part of a class and not of an object.
But, because of the notation we abuse by writing obj.staticMethod(), people generally get confused.
Thanks,
Aatish on December 7th, 2009 5:14 amhi…!
If you would use static methods and fields only, you would end up programming procedural like you would do with C or Pascal, loosing all the benefits of oop..so static method is used only when required and instance method is used elsewhere to get the benefit of oop.
Virat Gandhi on March 30th, 2010 8:56 amnice tutorial good job
java tutorial for beginners on October 28th, 2010 6:21 amIt is a nice website
Anonymous on November 29th, 2010 7:28 amvery helpful website
thanks alot
[...] Instance Variable Java variables that are declared without static keyword are instance [...]
How many types of java va&hellip on January 22nd, 2011 5:34 pm[...] As given in the last line, method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields. [...]
Java (JVM) Memory Types&hellip on January 22nd, 2011 5:37 pmA good tutorial !
Tri Nguyen Minh on March 28th, 2011 7:38 amThanks for sharing.
[...] initialization block. That too you have seen in it a class for initialization. You may have used a static initialization block. When you use the initialization block for an anonymous inner class it becomes [...]
Java Double Brace Initial&hellip on May 21st, 2011 11:27 amThanks to whomsoever it may concern..This is a good material,which seemed to be useful to me.
Ronak on May 24th, 2011 10:42 amit is not clearly, please provide full information with example
raghavareddy on June 2nd, 2011 5:45 am[...] To specifically denote that the instance variable is used instead of static or local variable.That [...]
Java this Keyword&hellip on June 10th, 2011 9:21 am@Anonymous:i cannot understand your point ll u please explain it
thanks
no access modifier – Subclasses in other package wont allow.
But in protected will can be accessed subclasses in another pacakge.
Muhammad Waqas on December 8th, 2011 7:08 pmAnonymous on October 14th, 2010 12:47 pm
By last point in java Instance Variables
When the ” value of a constant is known at compile time ” it is declared ‘final’ using the ‘static’ keyword.
i would like to ask one thing according to double quotes
there are how many to give a value to a variable or a Constant
1. Compile time
2….
3…
there are how many possible ways?
Muhammad Waqas on December 8th, 2011 7:22 pmthanks
what are static fields
Muhammad Waqas on December 8th, 2011 7:26 pmwhat about this
Class A{//something here}
public Class B{
static A obj= new A(); // a static object to //class
****
harry on February 6th, 2012 12:11 pmcan you explain this how this works?
[...] As given in the last line, method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields. [...]
Java Keyword & Some I&hellip on February 21st, 2012 5:30 pmnice one
indian on April 3rd, 2012 10:40 amHi Joe,
I have doubt about Static.
When we go for static and why we need static? In which situation we need to go for static.?.
Thanks a lot for your Great work. your blog helped me to clear all level interview.
Kannan on April 4th, 2012 10:19 amHi Kannan,
Static are basically class variable, they are not stored on the heap but in the special area called method area, unlike object which when allocated occupied area in heap.
Static variable are loaded just once when the class is loaded by the JVM at the same time static variable is also created. So all the content which will be common to all the method and object are stored as static variable.
Shazz on April 8th, 2012 7:47 pmShazz, thanks for replying to users questions.
Joe on April 9th, 2012 11:19 am[...] global access. It can be accessed from anywhere inside the thread. Also note that, it is declared static and [...]
ThreadLocal&hellip on April 22nd, 2012 11:00 pmwhat is the difference between class variable and static variable ?
mcajavaprogramer on April 30th, 2012 10:36 am[...] a final class and cannot be instantiated. Therefore all its memebers (fields and methods) will be static and we understand that it is an utility class. As per javadoc, “…Among the facilities [...]
System.out.println&hellip on May 1st, 2012 12:22 amHi Joe,
I am a Java newbie. This page is very helpful.
The question I have is about Static Methods is…..’Java static methods cannot access instance variables or instance methods directly.’
Why in the Main() method which is a Static Method, we can access other methods (part of that class) that are not defined as Static?
VJ on May 23rd, 2012 10:02 am