Apart from private, public and protected modifiers, there are other modifiers in Java that are applicable on fairly higher grounds. Since we are discussing a lot about modifiers, let us go through them from a panoramic point of view — later on, when we delve deep into server side programming and sample project, they will gain much more prominence. Static variables the highschool_student class, the variable group and the parent class variables name, age, classroom etc. will be allocated for each instance of a class. It is only because of this fact that we are able to store different set of variables in
But, we may have situations in which we want to allot only one variable for all Static variables are those that are declared static during declaration. Thus, apart Variables declared static are commonly shared across all instances of a class. When you Since there is only one copy of the variable available for all instances, the code
Static methods accessor and mutator methods of highschool_student class have been declared static. Static methods are those whose implementation is same for all instances of a class. For example, if you try to access the instance variable group inside a static method
Can't make a static reference to nonstatic variable group in class This simply means that group is an instance variable which cannot be accessed by the Another interesting dimension of static modifiers and methods is that they can be
Usage of static modifiers projects. Typically, in live projects, there may be many variables which represent the runtime All these variables can be declared static in one place (i.e. in one class) and can be Such environment variables are typically declared in a flat text file and are read into
Source code of highschool_student .java (revised again !) To compile this example type javac highschool_student.java To run this example type java highschool_student  public class highschool_student extends student { //Additional attribute protected String group; protected static String school_name; //Constructor method public highschool_student(String name, int age, int classroom){ //Just referring to the parent class constructor method super(name, age, classroom); } //Overloading Constructor method, with Group information public highschool_student(String name, int age, int classroom, String group){
//Refer parent class constructor for basic attributes super(name, age, classroom);
//Setting specific attribute of this class this.group = group; //this can also be done //school_name = "APSV High school"; }
//static accessor and mutator methods ! public static String get_schoolname(){ return school_name; } public static void set_schoolname(String school_name1){ school_name = school_name1;
//this code will give error — this is not permitted inside //this.school_name = school_name1; //this code will give error — accessing instance variable inside static method ! //group = "B"; } public String get_name(){ return name; }Â public void set_name(String name1){ this.name = name1; }
//Overriding the parent class method public boolean learning (String explanation){ //just a simple reply if ((explanation.length() > 0) && (explanation != null)) return true; else return false; } //Overloading the learning method public boolean learning (String explanation, String lesson){ //just a simple reply if ((explanation.length() > 0) && (lesson.length() >0)) return true; else return false; }Â public void lab_experiment (String experiment){ //implmenet the experiment process //no need to return anything }Â public static void main(String argv<>){ highschool_student.set_schoolname("APSV High School"); System.out.println("School Name :" + highschool_student ram = new highschool_student("ram", 16, 11); System.out.println("Ram's School Name :" + } } S Gokul is an e-commerce Consultant with Cybervalley Global Systems, This article originally appeared in CIOL on March 2001. |
Java: Static variables and methods
New Update