MainMenu

Home Core Java Overview Maven Tutorials

Saturday, 14 January 2017

Static keyword in Java with Example



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

Note : Static defined things can be accessed without creating the object for the class in which they are present. Example without static keyword :
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:
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:
Student name is :chandan
Student age is :27
Student college Name is :Shivaji Inter College
Tags :-

what is static keyword in java?, example of static keyword in java

2 comments:

  1. تعد شركة ركن الامانة (0505561372) من الشركات المتخصصة فى اعمال التنظيف و النقل و المكافحة بالمنطقة الجنوبية فى المملكة العربية السعودية فهى تقدم خدمات متميزة وعلى اعلى مستوى من الخبرة و التقنية بالاعتماد على عمال مدربين ومتميزين فى عمالهم ونقدم خدمات باقل الاسعار التى تناسب جميع عملائنا الكرام فتواصلوا معنا على رقم الشركة
    شركة تنظيف بخميس مشيط
    شركة تنظيف منازل بخميس مشيط
    شركة تنظيف كنب بخميس مشيط
    شركة تنظيف خزانات بخميس مشيط
    شركة عزل اسطح بخميس مشيط
    شركة عزل خزانات بخميس مشيط
    شركة كشف تسربات المياه بخميس مشيط
    شركة تسليك مجارى بخميس مشيط
    شركة مكافحة حشرات بخميس مشيط
    شركة نقل اثاث بخميس مشيط

    ReplyDelete