Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Static Object Vs Non-static object

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Greenhorn
Posts: 11
MyEclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...

 
reply
    Bookmark Topic Watch Topic
  • New Topic