aspose file tools*
The moose likes Beginning Java and the fly likes Static Object Vs Non-static object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Object Vs Non-static object" Watch "Static Object Vs Non-static object" New topic
Author

Static Object Vs Non-static object

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Hi,

I have a simple doubt. Pls clarify,



In the above coding, 't' will have its own copy of 'a' and 't1' will have its own copy of 'a'. Both 't' and 't1' will share the 'b'.

Pls consider the below coding and tell how the variables will be shared.

[CODE/]
[B]CODE:2[/B]

int a = 10;
static b = 20;

static Test t = new Test();
static Test t1 = new Test();
[/CODE]

Whether CODE 1 and CODE 2 are same, if not how?. If same how?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
They are different. In Code:1, each object of the class that code is contained in has their own objects t and t1, while in Code:2, there is only one object t and one object t1, both of which are shared between all objects of that class.


Ping & DNS - my free Android networking tools app
Qi Liang
Greenhorn

Joined: Jun 04, 2003
Posts: 28


are members of the class Test, right?

If so, 't' and 't1' in both CODE1 and CODE2 share one static b.

===============================================================

Where to go after seven years' development?
Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
If possible could you give one example that explains static object and non-static object behaviour.

Thanks for your reply and expecting the answer for the above thing!!
Narasimham Varanasi
Greenhorn

Joined: Apr 06, 2011
Posts: 11

Mark Henryson wrote:If possible could you give one example that explains static object and non-static object behaviour.

Thanks for your reply and expecting the answer for the above thing!!


@Ulf Dittmer

Thank you for the information .But still this concept is wage please help me out in this Static object VS Non_Static object
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8


This has created three instances of Test. a.testNonStatic and b.testNonStatic are two of them. a.testStatic and b.testStatic refer to the same object, which is the third. In practice, because testStatic is shared, you should never refer to it like this. Instead refer to StaticTest.testStatic.

Does that help?
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

what you have done is ..,

IN YOUR FIRST CODE one of the two instance variables are set to static

and the two objects have access to it ...

IN YOU SECOND CODE you have made the objects themselves static ... so now the scene is this ...



http://plainoldjavaobject.blogspot.in
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Object Vs Non-static object