Home  • Programming • Java

Variables types in Java

A class can contain any of the following variable types. Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. Class variables:Class variables are variables declared with in a class, outside any method, with the static keyword.
class Point{

  private int x; // instance variable
  private int y; // instance variable
  public static String coordinate; // Class variable

  Point(){
      this.x=0;
      this.y=0;
      this.coordinate=0+","+0;
  }
  Point(int _x,int _y){ // here _x and _y are local variables
      this.x=_x;
      this.y=_y;
      this.coordinate=_x+","+_y;
  }

  public void show(){
    String p="("+this.coordinate+")"; // here p is a local variable
    System.out.println("Cordinate(x,y)="+p);
  }

}

class Main{
  public static void main(String[] arg){
    Point p=new Point(3,4); 
    p.show();
    System.out.println(Point.coordinate); 

  }

}
Class variable can be called directly without creating class Object or instance. In this example coordinate is class variable
Note: Instance variable cannot be called without creating class object or instance. In this example x and y are instance variables

Comments 12


Your definition is correct, but in the example above, there is some cause to be hesitate, Actually your will be more clear if you kindly distribute the variable as member variable  and  the instance, other pattern will be classified based on variable scope known as local and global variable. On your example you have commented int x and y as instance, but actually they doesn't , they are member variable with the private  visibility mode, but the actual instance on your code is point p(at class main) where the class point has instantiated by the new operator(new is a reserved key word in java, some others are abstract, boolean, break, byte, char, cast, const, continue, default, do, double, jump, goto, finally, through, try, catch, private, protected, public, static, void etc in a count of 87, when i was studied)
sorry if the post hurts ! But my opinion is to study hard to learn at one time, never repeat it for the clear concept. it will pull you to the bound but time will never wait
If i am in a wrong way please  refer me the book name so, i can be cleared!
In a class member with private modifier can be access by public method so that other class can access. Here her example is i think ok but not details. Both static and without static variables are global member within a class. But uses is different.
To use class variable you don't need to create class instance. here in example  coordinate with public modifier
You cannot access x and y members even though if set as public modifier without creating class instance
If you use private visibility mode, i may guarantee you that u can't access this method or variable from the outside of that class
yea, exactly, i have found your msg but a little bit later
scope of the local variable is in the method body or in the method parameters.
thank this post
We access all private members by public methods of that class. Other classes does not allows to access private member directly.
Here "p" is the class instance or class object of Point class created by new keyword in Main class.
About Author
Zinia Islam
Copyright © 2024. Powered by Intellect Software Ltd