Answer to Question #248510 in Java | JSP | JSF for Tege

Question #248510

Explain the following variables with example

1, static variable

2, object variable

3, instance variable


1
Expert's answer
2021-10-10T13:23:06-0400

Static variable:

A static variable in java is a variable which belongs to the class and initiated only once at the time of starting of the execution of the program. It belongs to the class not to the objects. In this, the single copy can be shared by the all the instances of the class and it can be access by the class name.

static int b;

Object variable:

An object is a kind of container which holds the references of the specific instance of the class. It is also known as the "variable" or "member".

public class Test {
   public Test(String test) {
      // constructor
      System.out.println("Passed Name is :"+test);
   }

   public static void main(String []args) {
      //create an object Test
    Test myTest = new Test( "test" );
       }
    }

Instance variable:

Instance variable is kind of non-static variable which can be declared inside the class, outside the class, method or in a blocks.

class Test
    {
        // Static variable
        static int a; 
        // Instance variable
        int b;        
    } 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS