0% found this document useful (0 votes)
9 views2 pages

Static Keyword

The document explains the use of the static keyword in Java, highlighting its application with class-level variables, methods, and inner classes. It provides examples of static variables for memory management and counting object instances, as well as the use of the 'this' keyword in constructors and methods. The document includes sample code to illustrate these concepts and their outputs.

Uploaded by

Thunder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Static Keyword

The document explains the use of the static keyword in Java, highlighting its application with class-level variables, methods, and inner classes. It provides examples of static variables for memory management and counting object instances, as well as the use of the 'this' keyword in constructors and methods. The document includes sample code to illustrate these concepts and their outputs.

Uploaded by

Thunder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Static keyword is non access modifier and can be used with class level variables, methods, blocks and

nested/inner
classes.
Class Abc
{
static int a;
void m1()
{
Static int a; //wrong, local variable cannot be static.
}
}

Static variable is used for memory management.


Exam:
class Emp
{
int eid;
String name;
static String company = "IPS"; //variable which is common among all objects should be declared static
Emp(int eid, String name)
{
this.eid = eid;
this.name = name;
}
public static void main(String args[])
{
Emp e1 = new Emp(1,"xyz");

System.out.println(e1.eid+" "+e1.name);
}
}

Static variable is used as counter for counting number of objects created during program execution.
class CounterDemo
{
static int count;
{
count++;
}
public static void main(String args[])
{
CounterDemo e1 = new CounterDemo();
CounterDemo e2 = new CounterDemo();
CounterDemo e3 = new CounterDemo();
System.out.println("total created objects: "+count);
}
}
//usage of this
class Abc{
int a=20;

Abc()
{ this("pankaj");
System.out.print(" pateriya");
}
Abc(String name)
{
System.out.print(name);
}
Abc(Abc obj3)
{
System.out.println(++obj3.a);
}

void display(Abc obj2)


{
System.out.println("Display can be called by this keyword");
System.out.println(++obj2.a);
}

/*void*/ Abc show()


{
int a = 10;
System.out.println("\nlocal vari is given preferenced "+a);
System.out.println(" Instance variable can be accessed using this "+this.a);
this.display(this); //implicitly, this is the instance of current class Abc
Abc obj3 = new Abc(this);
return this;
}
}

public class Main


{
public static void main(String[] args) {
Abc obj = new Abc(); //this can not be used inside the main() because it is non static
System.out.println(++obj.show().a);
}
}

OutPut:
pankaj pateriya

local vari is given preferenced 10

Instance variable can be accessed using this 20

Display can be called by this keyword

21
22
23

You might also like