by Dinesh Thakur Category: Data Type

Static are the members or variables which are defined independently of any object of that class. Static members are the members that can be used by itself, without reference to a specific instance. We can declare both methods and variables to be static. The variables declared as static are global variables. When objects of the class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

Methods declared as static have several restrictions:

1. They can only call other static methods.

2. They must only access static data.

3. They cannot refer to this or super.

If we need to do computation in order to initialize the static variables, we can declare a static block which gets executed exactly once, when the class is first loaded.


         class StudentDetail
                 static int SrNo=1;
                 int RollNo,sci,math;
                 String Sname;
                 void Setdata()
                throws IOException
                       BufferedReader bf=new BufferedReader(new InputStreamReader(;
                       String str;
                       int RollNo = SrNo;
                       System.out.println("Enter Name of a Student : ");
                       System.out.println("Enter Science Marks ");
                       System.out.println("Enter Math Marks ");
                       void ShowDetail()
                            System.out.println("Roll Number :"+ RollNo);
                            System.out.println("Name : "+Sname);
                            System.out.println("Science :"+sci);
                            System.out.println("Math :"+math);
                       class StaticVariable
                               public static void main(String args[])
                                      int i;
                                              StudentDetail STD[]=new StudentDetail[3];
                                              System.out.println("Enter the Details of Three Students");
                                                       STD[i]= new StudentDetail();
                                                       System.out.println("The Details of Three Students Entered are :");
                                                    catch(IOException e){}

Java Static Variables Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content