java programming
types of variable in java
1) local variable
2) instant variable
3) static variable
LOCAL VARIABLE - a variable define as within block or method and constructor called as variable
/*package whatever //do not write package name here */
// Contributed by Avinash rathod
import java.io.*;
class Local {
public static void main(String[] args)
{
int var = 10; // Declared a Local Variable
// This variable is local to this main method only
System.out.println("Local Variable: " + var);
}
}
INSTANT VARIABLE - instant variable is not static variable his declare outside of class constructor method
// Online Java Compiler
class Instant {
public String avier ; // declared instant variable
public Instant(){ //default constructor
this.avier = "do something "; // initialize instant variable
}
public static void main(String[] args) { // main method
//int loc = 23;
Instant name = new Instant(); // object creation
System.out.println("avier name is :- " + name.avier); //o/p
}
}
STATIC VARIABLE - static variable is known as class variable
/*package whatever //do not write package name here */
import java.io.*;
class STR{
public static String stat = "Avinash rathod"; //Declared static variable
public static void main (String[] args) {
//stat variable can be accessed without object creation
//Displaying O/P
System.out.println("static Name is : "+STR.stat);
}
}
No comments:
Post a Comment