Newsletter

Static in Java, Static Variables, Static Methods, Static Classes

Core Java » on Oct 6, 2012 { 11 Comments } By Sivateja

Static in java  🙂 very important concept in interviews.  Let us see the story behind static variables and methods and classes in java classes….

Static Variables

public class MyStatic
{
    int var;
    static int var2;

    public static int methodStatic()
    {
      ------------
    }
}

Observe the above class having 2 variables, 1 is instance and other is static.  If we create multiple objects of  ‘MyStatic‘ class, always instance variables will be given separate space for storage, but static variable is common to all objects of MyStatic class, no separate space will be given to each object. Any java object belongs to MyStatic class can modify that static variable,  even there is no need of creating object of a class to modify the static variables, we can modify them by its class name directly.

All java instance methods can access these static variables, and always static variables will be initialized first and then instance variables.

Static Methods

Here also same concept, we can call all static methods of a java class by its class name some thing…..

MyStatic.methodStatic();

We no need to create object for MyStatic class, we can call directly just like above which is always good practice to do. In general we used these static methods to access static variables and hmm in practical we may touch these static methods in banking related applications (Account Creating Module) and even in almost all real time projects. All instance methods of a class can access these static methods, but static methods cannot access any instance types directly.

I mean all instance’ can access static, but static cannot access any instance types directly;)

Static Classes

Hope you know that we can create inner classes in java, in java only inner classes can declare as static and we used to call them as Nested classes.

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

11 Responses to “Static in Java, Static Variables, Static Methods, Static Classes”
  1. krish says:

    super explanation………..

  2. You have removed my confusion about instance and static members.
    thanks

    why wait,notify and notifyAll methods belong to object class?

    Please explain if you may?

  3. Jitendra says:

    Nice explanation in Simple language..Thank u!!!!

  4. Anand says:

    You haven’t discussed static blocks and their restriction. static blocks can be used to add external libraries. As far as inner classes are concerned we can declare inner classes even in interfaces, surprising but do not understand why?, how can we make use of inner classes in interfaces?.

  5. Reddappa says:

    Really very good Explanation.very nice

  6. santhoshreddy says:

    How to delete static variables?

  7. sayyed RAFIKH says:

    Sir your notes is best among all please issue core java notes pointwise thank you

  8. laxman says:

    very good explanation..
    Thank you..

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.