Static Object

How to create static object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package composition;
 
class Demo1
{
	int k=12;
	void test()
	{	
          System.out.println("running test method");
	}
}
 
class Sample
{
 
	static Demo1 rf1=new Demo1();// creating static object
 
	void disp()
	{
		System.out.println("running disp method");
	}
 
}
 
public class StaticObject
{
 
	public static void main(String[] args)
	{
 
		System.out.println("*****************************");
		System.out.println("k value: "+Sample.rf1.k);
		Sample.rf1.test();
 
		System.out.println("*****************************");
	}
}
output:
 
*****************************
k value: 12
running test method
*****************************

Leave a Reply

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