File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Object Vs Non-static object

 
Mark Henryson
Ranch Hand
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Qi Liang
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
Java MyEclipse IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 384
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic