Static Lazy Initializer - java

I have a class that configures a server. The server object is static and is lazy initialised. The issue is, some of the config for the server comes from non-static member variables of the containing class. Obviously, the non-static members cannot be accessed. Is there a way around this, that I can configure my server using non-static variables? The server must remain static.
public class ClassA {
private static MyServer myServer;
private int a;
private int b;
public ClassA(int a, int b) {
this.a = a;
this.b = b;
}
public static MyServer getMyServer() {
if(myServer == null) {
myServer = configureServer();
}
return myServer;
}
private static MyServer configureServer() {
MyServer myServer = new MyServer();
myServer.setaPlusB(a + b);
return myServer;
}
}
public class MyServer {
private int aPlusB;
public void setaPlusB(int aPlusB) {
this.aPlusB = aPlusB;
}
}

From your question, I understood it as something like below;
public class Server {
private class ServerImpl {
private int ab;
public ServerImpl() {
ab = Server.a + Server.b;
}
}
private static int a;
private static int b;
private static ServerImpl s;
static {
a = 10;
b = 10;
}
public static ServerImpl getServer(int newA, int newB) {
a = newA;
b = newB;
return getServer();
}
public static ServerImpl getServer() {
if (s == null) {
s = new ServerImpl();
}
return s;
}
}

Related

How to change the value of private variable in java

I started to programm in Java since Yesterday, and I have the biggest question of my entire programmer life(since Yesterday).
For example, let's say I have a code like this:
public class itsAClass {
static private String A;
public static void main() {
A = "This should be changed";
}
public String something() {
return A;
}
}
I wanted to use the method something() in another Class to get the String Sentence of A, but I got only null.
How can I change the value of A, so that the another Class can get the Value "This should be changed"?
If you just want to bring this code to work you just can make something() static as well.
But this will be not the right way to approach this problem.
If you want to hold code in the main class you could do something like this:
public class AClass {
private String a;
public static void main() {
AClass myC = new AClass();
myC.setA("This should be changed");
// than use myC for your further access
}
public String something() {
return a;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}
If you want to access it by a external class without direct reference you can checkout the singleton pattern.
public class AClass {
private final static AClass INSTANCE = new AClass();
private String a;
public static void main() {
getSingleton().setA("This should be changed");
}
public String something() {
return a;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public static AClass getSingleton() {
return INSTANCE;
}
}
This way you can access it via AClass.getSingleton() from any location of your code.
You have to call your main() function.
In another class:
itsAClass aClassObj = new itsAClass();
aClassObj.main();
// or rather itsAClass.main() as it is a static function
// now A's value changed
System.out.println(aClassObj.something());
the way to set the value of private variable is by setter and getter methods in class.
example below
public class Test {
private String name;
private String idNum;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setAge( int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
}
}
you can call method main() in method something().
public class itsAClass{
static private String A;
public static void main() {
A = "This should be changed";
}
public String something() {
main();
return A;
}
public static void main(String[] args){
itsAClass a1 = new itsAClass();
System.out.println(a1.something());// prints This should be changed
}
}

Storing and accessing objects in a class without a instance reference

I know in C# this is okay to do but what about in Java? I have tested it and it works but should it be avoided and if so, why?
public class A {
public A() {
B.set(this);
}
}
public final class B {
private static A a;
public static void set(A a) {
this.a = a;
}
public static A get() {
return a;
}
private B() {
}
}
In Java this
private static A a;
is called an Class field, it has the same value, or in this case points to the same object in every instance of this class. Also it can be accessed without created instance of the class, in this case B.get(). You can change this
public static void set(A a) {
this.a = a;
}
public static A get() {
return a;
}
to this
public static void set(A a) {
B.a = a;
}
public static A get() {
return B.a;
}

accessing private member of member class

I have a class A, with a private member int myMember. And a class B with a private member of the class A, called myA;
That is:
public class A{
private int myMember;
...
}
public class B{
private A myA;
}
I would like to be able to access:
B.myA.myMember;
but it seems I can't because myMember is private in A. The thing is, I need A to be defined as private for the purpose of the exercise (that also includes it can't be protected). Is there a way around this?
Thanks.
public class A {
private int myMember;
public int getMyMember() {
return myMember;
}
public void setMyMember(int myMember) {
this.myMember = myMember;
}
}
public class B{
private A myA;
public B() {
myA = new A();
myA.setMyMember(0);
int a = myA.getMyMember();
}
}
Use getters :
public class A {
private int myMember;
public getMyNumber() {
return myNumber;
}
}
public class B {
private A myA;
public A getA() {
return myA;
}
}
So now you can code :
B b = new B();
b.getA().getMyMember();
Since you've stated you can't create more public methods, aka getters, you could use reflection...
public class A{
private int myMember;
...
}
public class B{
private A myA;
private int get(){
try {
Field field = myA.getClass().getDeclaredField("myMember");
field.setAccessible(true);
return (int) field.get(myA);
catch (Exception e){
//Something went wrong, the field doesn't exist or a security exception
return null; //or return some "error number" like -10
}
}
}
If you can declare the private field as static then something like this is possible :
public class A {
private int myMember;
}
public class B {
public static void main (String[] args) {
int myMember = new A() {
public int getPrivate() {
return myMember;
}
}.getPrivate();
System.out.print("\n\t Id : " + myMember);
}
}

Accessing an Object from other Methods

How do I access an object, that was instantiated in the constructor, in another method? (e.g. object b below) What is the best way to instantiate this object so that all of my class methods have access to the same object?
public class ClassA{
private final int size;
public ClassA(int N){
size = N;
ClassB b = new ClassB(size);
}
public void doSomething(){
b.doSomething();
}
}
You just need to assign it to a field:
public class ClassA{
private final int size;
private final ClassB b;
public ClassA(int N){
size = N;
b = new ClassB(size);
}
public void doSomething(){
b.doSomething();
}
}
Define ClassB b as instance variable.
public class ClassA{
private final int size;
ClassB b;
public ClassA(int N){
size = N;
b = new ClassB(size);
}
public void doSomething(){
b.doSomething();
}
}
Declare it as a field just as you have done with size:
public class ClassA{
private final int size;
private final ClassB b;
public ClassA(int N){
size = N;
b = new ClassB(size);
}
public void doSomething(){
b.doSomething();
}
}
you can simply create a property/field of type B
public class ClassA{
private final int size;
private B bInstance;
public ClassA(int N){
size = N;
bInstance = new ClassB(size);
}
public void doSomething(){
b.doSomething();
}
}

error: no enclosing instance of type BPN is in scope {

class BPN {
public class BackpropagationNet extends NeuralNet
{
Vector neuronLayerVector;
NeuronLayer[] neuronLayerArray;
//...
public BackpropagationNet()
{
this.learningCycle = 0;
this.maxLearningCycles = -1;
//....
resetTime();
}
//some functions
void addNeuronLayer(int paramInt)
{//.... }
void connectLayers()
{//....}
}
abstract class NeuralNet
{
final int PATTERN_LENGTH = 100;
final int PATTERN_VALUE = 101;
final int PATTERNFILE_LENGTH = 102;
final int GENERAL_IO = 104;
//....
}
static BackpropagationNet bpn;
public static void main ( String[] args ) {
// some logic...
bpn = new BackpropagationNet();
//...
}
}
Well, this is short program which should demonstrate the problem.
When i try to compile i receive this error:
no enclosing instance of type BPN is in scope {(line 9)
remove static keyword from the class, top-level class cannot declared as static class.

Categories

Resources