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) |
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("**********************"); } }
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(); } }
Name : John Age : 6 SchoolCode : 5000 ********************** Name : Dave Age : 7 SchoolCode : 5000 ********************** Name : juli Age : 5 SchoolCode : 5000 **********************
https://sites.google.com/site/javaee4321/java/VariableDemoStaticClassNameAccessApp.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment