Accessing an Object from other Methods - java

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();
}
}

Related

Create a final field initialized by a child class

I have an abstract class with subclasses that share a method that increments a value by a specific increment and is bounded by a lower limit and an upper limit. Each subclass has its own increment and its own bounds These values should all be declared final, but if I declare them final how can they be initialized by the subclasses.
Below is the only way I could think of to get it to work, but it seems rather cumbersome.
public abstract class ClassA {
protected int LOWER_LIMIT;
protected int UPPER_LIMIT;
protected int INCREMENT;
public ClassA() {
}
}
public class ClassB extends ClassA {
public ClassB() {
super();
this.LOWER_LIMIT = 0;
this.UPPER_LIMIT = 4200;
this.INCREMENT = 15;
}
}
public class ClassC extends ClassA {
public ClassC() {
super();
this.LOWER_LIMIT = 0;
this.UPPER_LIMIT = 99;
this.INCREMENT = 1;
}
}
The quite common pattern is this. You also force the subclasses to define the value.
public abstract class Base {
final protected int field1;
final protected int field2;
final protected int field3;
protected Base(int field1, int field2, int field3) {
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
}
public class Subclass extends Base {
public Subclass () {
super(0, 4200, 15);
}
}

Static Lazy Initializer

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;
}
}

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;
}

howto handle objects of private class, got as parameter

I learn Java at university and I have to do following excercise.
(simplified example)
import java.util.*;
public class A{
private static class B{
Integer b;
private B(int b){this.b = b;}
}
private static class B_Comparable extends B implements Comparable<B_Comparable> {
private B_Comparable(int b){super(b);}
#Override
public int compareTo(B_Comparable that) {
return this.b.compareTo(that.b);
}
}
private static class C<T> implements myList<T> { // see below
private ArrayList<T> lst = new ArrayList<>();
private static C<B_Comparable> createComparable() {
C<B_Comparable> ust = new C<B_Comparable>();
for (int i =0; i < 9; i++)
ust.lst.add(new B_Comparable(i));
return ust;
}
#Override
public T fetch(int index){
return lst.get(index);
}
}
private void test(){
C<B_Comparable> ustComparable = C.createComparable();
A result = ClassD.handle(ustComparable,3,4);
}
}
//--------------------------------------------------------
public class ClassD{
public static <T, S> T handle( S ustC, int pos1, int pos2 ){
// how can I compare elems of object ustC ?
ustC.fetch(pos1).compareTo(ustC.fetch(pos2));
//how can I fetch obj at pos1 ?
return ustC.fetch(pos1);
}
}
//-----------------------------------------
public interface myList<T> {
T fetch(int index);
}
static method handle gets an object (ustC) which is private. How can I
use methods, compareTo and fetch for this object? I have tried parametrisation, but if its the right way, I don't know how to solve.
Thanks for any help.
As discussed in comments, ustC, by virtue of the way handle is called in this context is of type C, which implements the myList interface. This interface exposes the fetch method, and is visible to your handle method.
The modification you arrived at in your comments would allow you to call fetch:
//Solution
public class ClassD {
public static <S extends Comparable> S handle(myList<S> ustC, int pos1, int pos2 ){
int y = ustC.fetch(pos1).compareTo(ustC.fetch(pos2));
return ustC.fetch(pos1);
}
}

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);
}
}

Categories

Resources