Pages

Wednesday, December 2, 2020

Variable Types in Java

Variable Types in Java

Variable is an prominent in Java programming language. No class is in Java api has developed without variable. So we can understand the significance of Variables and its types.
Variable is a name to hold the values in the memory and how to handle the data operations.

Variable Types:

Following are the variable types.

1) Local variable
2) Instance variable
3) Class variable or Static variable


Variable Types in Java



Below example is from Java api class "CleanupThread" and it has several variables. We will discuss about this at end of this tutorial. We will see first all types of variables.


 

class CleanupThread
  extends Thread
{
  private ArrayList nonJarItemRemovalList = new ArrayList();
  private ArrayList jarItemRemovalList = new ArrayList();
  private ArrayList loadedResourceList = new ArrayList();
  private static final double CACHE_THRESHOLD_PERCENT = 0.98D;
  private final Object syncObject;
  private final long currentCacheMaxSize = Config.getCacheSizeMax();
  private volatile long currentCacheSize = 0L;
  private boolean initCacheSize = true;
  private static final long BUSY_STALE_LIMIT = 3600000L;
  
  CleanupThread(String paramString, Object paramObject)
  {
    super(paramString);
    setDaemon(true);
    this.syncObject = paramObject;
  }
}

different types of Variables in Java

1) Local variables:


Local variables represent which has limited scope to particular area. These will be created upon method invocation and destroyed when exists.

Case a : Local variable can be applied in methods, constructors and normal blocks.

package com.java.w3schools.core.variables.local;

public class LocalVariablesA {

 public LocalVariablesA(int constructorLocalVariable) {

  constructorLocalVariable = constructorLocalVariable + 10;

  System.out

    .println("methodLocalVariable :: " + constructorLocalVariable);

 }

 {

  int blockVariable = 2;

  System.out.println("blockVariable :: " + blockVariable);

 }

 public void methodVarable() {

  int methodVariable = 3;

  // System.out.println("inside method blockVariable :: "+blockVariable);

  // Here we get compile time error(blockVariable cannot be resolved

  // to a variable)

  System.out.println("methodVariable :: " + methodVariable);

 }

 public static void main(String[] args) {

  LocalVariablesA a = new LocalVariablesA(10);

  a.methodVarable();

 }

}



Output :

blockVariable :: 2
methodLocalVariable :: 20
methodVariable :: 3

Case b: Access modifiers can not be applied on local variables

package com.java.w3schools.core.variables.local;

public class LocalVariableB {

 public void methodLocalB(){


  public int height = 100; // compile time error

 }

}


Error: Illegal modifier for parameter height.


case c: Local variable should be initialized before using them

package com.java.w3schools.core.variables.local;

public class LocalVariableB {

 public void methodLocalB() {

  int height;


  System.out.println(height); // Compile time error

 }

}



Error : The local variable height may not have been initialized

Instance Variable:

Instance variables are declared in class, all instance variables are accessible in any method or constructor or block of class.

These are initialized when object is created for class. Each object of class will have separate copy of instance variable in heap memory.

Access modifiers can be applied on instance variables.

These are initialized to default values by default. Eg. if int is to "0", String to null, etc..

Case d:
 

package com.java.w3schools.core.variables.instance;

public class Student {

 public int studentId;

 public String name;

 public int age;

 // here we have created three instance variable. All these can be accesible

 // in all methods of this class.

 public int getStudentId() {

  return studentId;

 }

 public void setStudentId(int studentId) {

  this.studentId = studentId;

 }

 public String getName() {

  return name;

 }

 public void setName(String name) {

  this.name = name;

 }

 public int getAge() {

  return age;

 }

 public void setAge(int age) {

  this.age = age;

 }

}


Static or Class variables:


Variables are declared at class level with keyword "static" is called as Static variables.

Only static method can access static variables whereas non static can not access them. But constructor can access static variables.

These variables are not eligible for Garbage collection.

If we create 100 objects for a class, all objects will share a common allocated memory. If one object has changed value of it than all objects will have updated value.

Refer Complete discussion on Static methods in Java.


Case e:
 

package com.java.w3schools.core.variables.statics;

public class Student {

 public String studentName;

 public static String schoolName; // Static variable


 public Student(String name, String studentSchoolName) {

  studentName = name;

  schoolName = studentSchoolName;

 }

 public String getStudentName() {

  return studentName;

 }

 public void setStudentName(String studentName) {

  this.studentName = studentName;

 }

 public String getSchoolName() {

  return schoolName;

 }

 public void setSchoolName(String schoolName) {

  this.schoolName = schoolName;

 }

}


 

package com.java.w3schools.core.variables.statics;

public class StudentMain {

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  Student student1 = new Student("Paul", "George School");

  Student student2 = new Student("Peter", "George School");


  System.out.println("Before update.. student1 school name :: "+student1.getSchoolName());

  System.out.println("Before update.. student2 school name :: "+student2.getSchoolName());


  student2.setSchoolName("Oxford");


  System.out.println("------------------------------------------------------------------");

  System.out.println("After update.. student1 school name :: "+student1.getSchoolName());

  System.out.println("After update.. student2 school name :: "+student2.getSchoolName());


 }

}




Output :

Before update.. student1 school name :: George School
Before update.. student2 school name :: George School
------------------------------------------------------------------
After update.. student1 school name :: Oxford
After update.. student2 school name :: Oxford


Here, we have updated school name to only student2. But in the output student1 also has updated to new values. Static variables shares common memory for all instances of the class.

Java api class:

 

class CleanupThread
  extends Thread
{
  private ArrayList nonJarItemRemovalList = new ArrayList(); // instance variable
  private ArrayList jarItemRemovalList = new ArrayList();
  private ArrayList loadedResourceList = new ArrayList();
  private static final double CACHE_THRESHOLD_PERCENT = 0.98D; // static variable
  private final Object syncObject;
  private final long currentCacheMaxSize = Config.getCacheSizeMax();
  private volatile long currentCacheSize = 0L;
  private boolean initCacheSize = true;
  private static final long BUSY_STALE_LIMIT = 3600000L;
  
  CleanupThread(String paramString, Object paramObject)
  {
  // paramString is local variable
    super(paramString);
    setDaemon(true);
    this.syncObject = paramObject;
  }
}



No comments:

Post a Comment

Please do not add any spam links in the comments section.