Static keyword in Java rcub

 static keyword in Java 


static is a keyword it can be used with variables, methods and blocks.

static data  or static variable:-
If you declare any variable as static, it is known as a static variable. 
the static variable can be used to refer to the common property of all objects ..
The static variable gets memory only once in the class area at the time of class loading.

Syntax : 

static <data-type> <variable-name >;


Advantages of static variable
---> it saves memory

example:
public class student
{
int rno;
String name;
static String college="Asp";
public student()//default
{
}
public student( int r,String n)
{
rno=r;
name=n;
}
public void display()
{
     System.out.println("roll no:"+rno);
     System.out.println("name:"+name);
     System.out.println("college   name:"+college);
}
public static void main(String args[])
{
student s=new student();
student s1=new student(10,"xyz");
s1.display();
student s2=new student(11,"abc");
s2.display();
}
}
output:
roll no:10
name:xyz
college name:Asp
roll no:11
name:abc
college name:Asp

static method:
If you apply static keyword with any method, it is known as static method.

A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance(object) of a class.
A static method can access static data member and can change the value of it.

Methods declared as static have several restrictions:
• They can only call other static methods.
• They must only access static data.
• They cannot refer to this or super in any way. 

Syntax :

static <datatype> <method-name>(  <parameters> ) 

{ 

} 

Example of static method.
public class student
{
int rno;
String name;
static String college="Asp";//static data or variable
public static void change()
{ 
     college = "kcp"; 
} 
public student()//default
{
}
public student( int r,String n)
{
rno=r;
name=n;
}
public   void display()
{
     System.out.println("roll no:"+rno);
     System.out.println("name:"+name);
     System.out.println("college name:"+college);
}
public static void main(String args[])
{
student s=new student();
student s1=new student(10,"xyz");
s1.display();
student.change();
student s2=new student(11,"abc");
s2.display();
}
}

output:
roll no:10
name:xyz
college name:Asp
roll no:11
name:abc
college name:kcp

static block:-
it Is used to initialize the static data member.
It is executed before the main method at the time of classloading.


Example of static block
class A{
 
  static{
System.out.println("static block is called ");
  }
  public static void main(String args[])
{ 
   System.out.println(" main method called"); 
  } 
} 

Output:
static block is called
main method called

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.