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

Student 1

static class example

Uploaded by

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

Student 1

static class example

Uploaded by

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

//program to show the use of static variable,static block,static method

package class_object;
class Student123 {

int rno; //non-static variable


String name; //non-static variable

static String clgname; //static variable(common property which is


shared by all the objects which we created in the program

static //static block=>for


initializing static data members
{
clgname="seven_mentor";

System.out.println("static block executed first...");


System.out.println(); //prints balnk line after print
statement

Student123(int r,String nm) //constructor


{
rno = r;
name = nm;
}

void display()
{

System.out.println(rno+" "+name+" "+clgname);


}

static void change() // a static method can access static only static
data members only
{

clgname="SM"; //changes the value of clgname to "SM" from


"seven_mentor"
}
}

public class Student1


{

public static void main(String[] args) {


// TODO Auto-generated method stub

Student123.change(); //static methods are always called


by the class name instead of object name

Student123 s1=new Student123(01,"suraj"); //all 4 s1,s2,s3,s4


objects can share common property i.e clgname and there is no need to call this
common property i.e static var. It is automatically call when program executes
Student123 s2=new Student123(02,"abc");
Student123 s3=new Student123(03,"pqr");
Student123 s4=new Student123(04,"suraj");
s1.display();
s2.display();

s3.display();
s4.display();

}
}

//output:

static block executed first...

1 suraj SM
2 abc SM
3 pqr SM
4 suraj SM

You might also like