0% found this document useful (0 votes)
3 views7 pages

Static This Program

The static keyword in Java is used for memory management and can be applied to variables, methods, blocks, and nested classes, indicating that they belong to the class rather than an instance. Static variables hold common properties for all objects, while static methods can be invoked without creating an instance and can only access static data. The 'this' keyword refers to the current object and is used to resolve ambiguity between class attributes and parameters, invoke constructors and methods, and pass arguments.

Uploaded by

rishavthakkar02
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)
3 views7 pages

Static This Program

The static keyword in Java is used for memory management and can be applied to variables, methods, blocks, and nested classes, indicating that they belong to the class rather than an instance. Static variables hold common properties for all objects, while static methods can be invoked without creating an instance and can only access static data. The 'this' keyword refers to the current object and is used to resolve ambiguity between class attributes and parameters, invoke constructors and methods, and pass arguments.

Uploaded by

rishavthakkar02
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/ 7

static keyword

The static keyword in Java is used for memory management mainly. We can apply static
keyword with variables, methods, blocks and nested classes. The static keyword belongs to
the class than an instance of the class.

The static can be:

1. Variable (also known as a class variable)


2. Method (also known as a class method)
3. Block
4. Nested class

static variable
o The static variable can be used to refer to the common property of all objects (which
is not unique for each object), for example, the company name of employees, college
name of students, etc.
o The static variable gets memory only once in the class area at the time of class
loading.

class Stud{
static int scount=0;//will get memory only once and retain its value
int roll;
Stud(int r)
{
roll=r;
scount++;//incrementing the value of static variable
System.out.println(“Student count”+count+”Roll no. ”+roll);
}
void disp()
{
System.out.println(“Student count”+count+”Roll no. ”+roll);
}
public static void main(String args[]){
//creating objects
Stud c1=new Stud(5); //op-1 5
Stud c2=new Stud(7); // 2 7
Stud c3=new Stud(15); //3 15
C1.disp(); //op- 3 5
C2.disp();
}
}

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

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

class Calculate{
static int cube(int x)
{
return x*x*x;
}
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
}
}

There are two main restrictions for the static method. They are:

1. The static method cannot use non static data member or call non-static method
directly.
2. this and super cannot be used in static context.

class A{
int a=40;//non static

public static void main(String args[]){


System.out.println(a); // Compile Time Error

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

class A2{
static{System.out.println("static block is invoked");}
public static void main(String args[]){
System.out.println("Hello main");
}
}
Non static block-

The need for a non-static block is to execute any logic whenever an object is created
irrespective of the constructor.
The Non-static blocks are automatically called by the JVM for every object creation in the java
stack area.
Example
public class NonStaticBlockTest
{

{ System.out.println("Non-Static Block-Object Created"); } // non-static block

NonStaticBlockTest() {
System.out.println("Execution of a Constructor"); // Constructor
}

public static void main(String args[])


{
NonStaticBlockTest nsbt1 = new NonStaticBlockTest();

//OP- Non-Static Block-Object Created


Execution of a Constructor

NonStaticBlockTest nsbt2 = new NonStaticBlockTest();

//OP- Non-Static Block-Object Created


Execution of a Constructor

}
}

this Keyword

The this keyword refers to the current object in a method or constructor.


The most common use of the this keyword is to eliminate the confusion between class attributes
and parameters with the same name.
this can also be used to:
 Invoke current class constructor
 Invoke current class method
 Return the current class object
 Pass an argument in the method call
 Pass an argument in the constructor call

Example1-Ambiguity Variable Names

public class MyClass {


int x;

// Constructor with a parameter


public MyClass(int x)
{
x = x; //this.x=x should be used
}

public static void main(String[] args)


{
MyClass myObj = new MyClass(5);
System.out.println("Value of x = " + myObj.x); //OP- 0
}
}

Example2-To invoke constructor (Constructor chaining )

class Stud
{
int marks;
int roll;

Stud(int roll)
{this.roll=roll;}
Stud(int roll,int marks)
{
this.marks=marks;
this(int roll); //error
}
}
class TestThis{
public static void main(String args[])
{
Stud a=new Stud(10,95);
}}

Example-‘this’ keyword to invoke current class method

class Test {

void display()
{
// calling function show()
this.show();
System.out.println("Inside display function");
}
void show() {
System.out.println("Inside show funcion");
}
public static void main(String args[]) {
Test t1 = new Test();
t1.display();
}
}
Example Program

class Student
{
static int scount=0; //static variable
int roll;
String name;
double cgpa;

Student(String name,double cgpa)


{
scount++;
this.roll=scount;
this.name=name; //use of this to identify class variable
this.cgpa=cgpa;
}
Student(String name) //constructor overloading
{
scount++;
this.roll=scount;
this.name=name;
this.cgpa=0;
}
Student()
{
scount++;
this.roll=scount;
this.name="NOR";
this.cgpa=0;
}
public void calRes(double m)
{
cgpa=(cgpa+m)/2;
System.out.println("updating CGPA for "+this.name);
this.disp();
}
public void calRes(double p,double t) //method overloading
{
double avg=(p+t)/2;
cgpa=(cgpa+avg)/2;
System.out.println("updating CGPA for "+this.name);
this.disp(); //this to call method
}
void disp()
{
System.out.println(roll+" " + " "+name+ " " +cgpa);
}
public static Student comp(Student ob,Student ob1) //object as parameter in static metod
{
if(ob.cgpa>=ob1.cgpa)
return ob;
else
return ob1;
}
}
public class Main{
public static void main(String args[])
{
Student s=new Student("ABC",8.5);
Student s1=new Student("xyz");
Student s2=new Student();
s.disp();
s1.disp();
s2.disp();
s.calRes(8);
s1.calRes(8,7.5);
s2.calRes(10,8);
Student s3=Student.comp(s1,s2); //call static method with class name
System.out.println("student with higher marks");
s3.disp();
}
}
Output-
1 ABC 8.5
2 xyz 0.0
3 NOR 0.0
updating CGPA for ABC
1 ABC 8.25
updating CGPA for xyz
2 xyz 3.875
updating CGPA for NOR
3 NOR 4.5
student with higher marks
3 NOR 4.5

You might also like