Java Static

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 pm

Good, 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 am

hi…!

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 am

nice tutorial good job

java tutorial for beginners on October 28th, 2010 6:21 am

It is a nice website
very helpful website
thanks alot

Anonymous on November 29th, 2010 7:28 am

[...] 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 pm

A good tutorial !
Thanks for sharing.

Tri Nguyen Minh on March 28th, 2011 7:38 am

[...] 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 am

Thanks to whomsoever it may concern..This is a good material,which seemed to be useful to me.

Ronak on May 24th, 2011 10:42 am

it 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.
Anonymous on October 14th, 2010 12:47 pm

Muhammad Waqas on December 8th, 2011 7:08 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?
thanks

Muhammad Waqas on December 8th, 2011 7:22 pm

what are static fields

Muhammad Waqas on December 8th, 2011 7:26 pm

what about this
Class A{//something here}
public Class B{
static A obj= new A(); // a static object to //class

****
can you explain this how this works?

harry on February 6th, 2012 12:11 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home