Categories
Java

Static Properties and Methods

Static Properties

1.It will be created commonly only once for all objects.

2.It will be initialized only once when the first object is created , further initialization is not permitted.

3.It is visible within the class but lifetime is throughout the program.

Static Methods

1.It can access only static members.

2.It can be invoked using class name.

In the following java program,static properties and methods are used.

import java.io.*;
class StaticExample
{
int a,b;
static int c;
void get()throws Exception
{
System.out.println("Enter 3 numbers");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
}
void show()
{
System.out.println(a+"\t"+b+"\t"+c);
}
static void disp()
{
System.out.println(c);
}
}
class StaticExampleMain
{
public static void main(String arg[])throws Exception
{
StaticExample s1=new StaticExample();
StaticExample s2=new StaticExample();
StaticExample s3=new StaticExample();
s1.get();
s2.get();
s3.get();
s1.show();
s2.show();
s3.show();
StaticExample.disp();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *