Tuesday 15 September 2015

Java Tutorial : Java Static Variable (Access Using ClassName)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=UXPpgzYfNYo&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Static Variable (Access Using ClassName) 
Student.java
public class Student
{
    String name; // instance variable
    int age; // instance variable
    
    static int schoolCode = 1000; // Static Variable
    
    void printInstanceAndStaticVariables()
    {
        System.out.println("Name : "+ name);
        System.out.println("Age : "+ age);
        System.out.println("SchoolCode : "+ schoolCode);
        System.out.println("**********************");
    }
}
StudentDemo.java
public class StudentDemo
{

    public static void main(String[] args)
    {
        Student john = new Student();
        john.name = "John";
        john.age = 6;
        
        /*
         * Using Class name we can access class variable
         * or static variable 
         */
        
        Student.schoolCode = 5000;
        

        Student dave = new Student();
        dave.name = "Dave";
        dave.age = 7;
        
        
        Student juli = new Student();
        juli.name = "juli";
        juli.age = 5;
        
        
        john.printInstanceAndStaticVariables();
        dave.printInstanceAndStaticVariables();
        juli.printInstanceAndStaticVariables();
    }

}
Output
Name : John
Age : 6
SchoolCode : 5000
**********************
Name : Dave
Age : 7
SchoolCode : 5000
**********************
Name : juli
Age : 5
SchoolCode : 5000
**********************
To Download VariableDemoStaticClassNameAccessApp Project Click the below link
https://sites.google.com/site/javaee4321/java/VariableDemoStaticClassNameAccessApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment