• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to access another class having private constructor using it's static variavble ?

 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LOGIC PLEASE!!! Thank you in advance.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ClassA{
static int a=3;
private ClassA(){}
}

public class ClassB{
static int b = 0;
public static void main(String arg[]){
b = ClassA.a;
System.out.println(b);
}

}
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raghava dv wrote:class ClassA{
static int a=3;
private ClassA(){}
}

public class ClassB{
static int b = 0;
public static void main(String arg[]){
b = ClassA.a;
System.out.println(b);
}

}



if there is a method in ClassA then how can i access it in ClassB using this static instance variable means i want to use this static instance variable to create object of ClasssA .
 
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitesh, I believe the situation you are asking for is often seen in the Singleton pattern.

In my example, SimpleSingleton has a private contructor, and an instance (the only instance of the class) is held in a static variable. An accessor method, getInstance(), allows other classes to retrieve this instance, although you could avoid using a method and just set the static variable (theInstance) to public so it could be retrieved directly if you really wanted (I wouldn't recommend that though).


output:
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Eli said.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:What Eli said.



Yes I got that but i want to use static instance variable to create that class object not static reference variable, how ???
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eli Wood wrote:Nitesh, I believe the situation you are asking for is often seen in the Singleton pattern.

In my example, SimpleSingleton has a private contructor, and an instance (the only instance of the class) is held in a static variable. An accessor method, getInstance(), allows other classes to retrieve this instance, although you could avoid using a method and just set the static variable (theInstance) to public so it could be retrieved directly if you really wanted (I wouldn't recommend that though).


output:
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.




from k&B
Rules for Constructors

Constructors can use any access modifier, including private. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.)


in above example you're using static method that allows to an instance creation that is correct but how using static variable as written above ??
 
Eli Wood
Ranch Hand
Posts: 37
Oracle Tomcat Server Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below uses a public static variable instead of a public static method. I believe it shows how this is possible to do, and why I would consider it to be a bad idea.

Output:
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.
Hello from an instance method! Object is sample.SimpleSingleton@82ba41.
This is going to be ugly....
Exception in thread "main" java.lang.NullPointerException
at sample.SingletonDriver.main(SingletonDriver.java:17)
 
Greenhorn
Posts: 24
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually if you make a little bit change in Eli's code you can achieve what you want to see. You can see the necessary changes below:

private static SimpleSingleton theInstance = new SimpleSingleton(); to public static SimpleSingleton theInstance = new SimpleSingleton();
SimpleSingleton s1 = SimpleSingleton.getInstance(); to SimpleSingleton s1 = SimpleSingleton.theInstance;

This is what you want to see; but not a good implementation regarding encapsulation issues!

I was writing this post but obviously Eli made it quicker than me
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Eli & Riza
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic