Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static reference var & singleton

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have one doubt in static.

I know the instance variables are one per object.
And the Static Variables are one per class.

I have used like below:
int i = 10; //Instance variable
static int i = 10; // Static Variable.

I comw across like this:
static Dog d = new Dog();

Pls explain the above line and its importance. And also I come across this cdoings in Singleton class and whether inheritance is possible in the singleton class. Since we are marking construtor as private. pls explain that too
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If possible provide me some sample codings!!

Thanks!!
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried some codings. pls go thru it.



Correct me,if i am wrong: In the above pgm.



If possible like above what will be the result for:
static ex e3 = new ex();
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're on the right track here. A third instance will get its own copy of a,b and share c again.

If your class will modify c you have a great reason to read up on threads and synchronize.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic