For Video Tutorial : Move on Youtube Channel
Note : Select the playlist as per your need & move with number sequence
Static keyword in Java with Example
Video for this tutorial : CLICK HERE
Static keyword in java is used to declare methods, variable & inner class as static to manage the memory.
We can define following things as static :
1.Data members
2.Methods
3.Blocks
4.Inner Class
package csc;
public class College
{
String sname;
int Age;
String collegeName;
public void details(String s1, int a1, String s2)
{
System.out.println("Student name is :" +s1);
System.out.println("Student age is :" +a1);
System.out.println("Student college Name is :" +s2);
}
public static void main(String args[])
{
College obj = new College();
obj.details("chandan", 27, "Shivaji Inter College");
}
}
Output:
Student name is :chandan
Student age is :27
Student college Name is :Shivaji Inter College
In above example, every time we are cr ating an object the instance data member "collegename" is getting a seprate memory each time and value of this valirable will be same all the time, hence we are allocating memory to this variable each time an object is created.
To overcome this problem we will define variable as static due to this static variable will get the memory only once in the life cycle of class.
Example with static keyword :
package csc;
public class College
{
String sname;
int Age;
static String collegeName = "Shivaji Inter College";
public void details(String s1, int a1)
{
System.out.println("Student name is :" +s1);
System.out.println("Student age is :" +a1);
System.out.println("Student college Name is :" +collegeName);
}
public static void main(String args[])
{
College obj = new College();
obj.details("chandan", 27);
}
}
Output:{
String sname;
int Age;
static String collegeName = "Shivaji Inter College";
public void details(String s1, int a1)
{
System.out.println("Student name is :" +s1);
System.out.println("Student age is :" +a1);
System.out.println("Student college Name is :" +collegeName);
}
public static void main(String args[])
{
College obj = new College();
obj.details("chandan", 27);
}
}
Student name is :chandan
Student age is :27
Student college Name is :Shivaji Inter College
Example with static keyword in method : In the above example if we declare our method(details) static then we can access it without using the object.
package csc;
public class College
{
String sname;
int Age;
static String collegeName = "Shivaji Inter College";
public static void details(String s1, int a1)
{
System.out.println("Student name is :" +s1);
System.out.println("Student age is :" +a1);
System.out.println("Student college Name is :" +collegeName);
}
public static void main(String args[])
{
details("chandan", 27);
}
}
Output:public class College
{
String sname;
int Age;
static String collegeName = "Shivaji Inter College";
public static void details(String s1, int a1)
{
System.out.println("Student name is :" +s1);
System.out.println("Student age is :" +a1);
System.out.println("Student college Name is :" +collegeName);
}
public static void main(String args[])
{
details("chandan", 27);
}
}
Student name is :chandan
Student age is :27
Student college Name is :Shivaji Inter College
Tags :-
No comments:
Post a Comment