This site javatpoint is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.

Tuesday, 31 October 2017

static variable in Java

Static variable in Java 


Static variable mainly used to the memory management. When we can declare variable as A static then less memory will be occupied. Methods and variables define inside class known as instance variables, that a separate copy of them is created upon the creation of each new objects.
Static variables are declared with static keyword. The main ()method is must be called static method because it must be called before any object exist. If we created any variable as a static then can't require objects of class.
Static variables are always access to the class name.

Syntax :

Static data_type variable name;

IN the statement static refers to the keyword,  data_type refers to the primitive data type and variable_name declare refers the variable define to the user. For example
Static  int mum;

Where static is keyword and Int is data_type num is the data type



   class   student
{
                    int hindi,english,maths,tot;
float per;
static int max;
void show()
{
tot=hindi+english+maths;
System.out.println(tot);
per=(tot*100)/300;
System.out.println(per);
}



}
class A01
{
                    public static void main(String args[])
{
student  rohit=new student();
student viren=new student();
rohit.hindi=77;
rohit.english=75;
rohit.maths=98;
rohit.max=300;
  rohit.tot=rohit.hindi+rohit.english+rohit.maths;
viren.hindi=87;
viren.english=68;
viren.maths=59;
viren.tot=viren.hindi+viren.english+viren.maths;
rohit.show();
viren.show();
}

}



Explanation : In the program the class name is student and it has eight members. The variable max is declare as a static,  that means no copy's for max variable in a class, and it can't be require to writen more than one. It can also be access to the class name.





C:\Users\master>javac A01.java

C:\Users\master>java A01
250
83.0
214
71.0

C:\Users\master>



Adbox