1

My java is a bit rusty, and I have to implement a solution for this problem.

Given in A.java where:

public class A implements Cloneable{
  protected DataStorage Y = new DataStorage();

  public A(DataStorage X, about 20 other inputs of different Object types){
   this.Y=X;
  }

  public static DataStorage getData(){
   return this.Y;
  }
}

My goal in another file ServletA.java where I want to retrieve the object Y or X. By doing a static call from A.java class... does it work or is there a better way?

public class ServletA{
  DataStorage Z = A.getData();
}

Note: Yes, this doesn't compile, but is there a solution around it?

My constraints that I have for the solution:

  • to make minimal changes to A.java because it is legacy codebase
  • there are many 20+ inputs for the A() constructor which is a nightmare
  • and my architect doesnt want to touch it either by changing A into an abstract class
  • cannot create an instance of A in ServletA.java

EDIT1: my question is if there is a solution to access X from ServletA.java WITHOUT creating an instance of A in ServletA.java. The above code is an example to show that i have thought of a method, but it doesnt work.

EDIT 2: if you want to downvote, please ask me for clarifications as courtesy. thanks!

PherricOxide
  • 15,493
  • 3
  • 28
  • 41
bouncingHippo
  • 5,940
  • 21
  • 67
  • 107
  • 1
    `Y` being instance specific, you need an instance of `A` to access it. – assylias Jul 23 '12 at 15:41
  • constraint of solution...cannot create ionstance of A in ServletA.java... – bouncingHippo Jul 23 '12 at 15:42
  • Lukas, my question is if there is a solution to access X from ServletA.java WITHOUT creating an instance of A in ServletA.java – bouncingHippo Jul 23 '12 at 15:42
  • each instance of `A` holds a `Y`. Without an instance of `A`, there is no `Y`. – assylias Jul 23 '12 at 15:43
  • 2
    Your getData method should not be static. Also, are the 20 other inputs of the same type? If so, you can use var args. If there are so many arguments, you can create a data object to pass the variables. – Sid Jul 23 '12 at 15:43
  • they're different Object types.. – bouncingHippo Jul 23 '12 at 15:44
  • I'm still confused by your question. X is local to the A constructor, so it can only be accessed there. Y is a member of A, so it can only be accessed in a member (non-static) method. For clarification, what is the reason that you cannot create an A instance in ServletA? – Code-Apprentice Jul 23 '12 at 15:50
  • CodeGuru, reason is that it is a legacy code and clients dont want to touch certain parts of it... – bouncingHippo Jul 23 '12 at 15:51

4 Answers4

1

If you only have one instance of A you should use the singleton pattern. Your ServletA code should be something like

public class ServletA {
  DataStorage Z = A.getInstance().getData();
}
sergiofbsilva
  • 1,606
  • 15
  • 20
  • public DataStorage getData(){ return this.Y; } private static final Options AClone = new A(); public static A getInstance() { return optionClone; } – bouncingHippo Jul 23 '12 at 17:37
1

You can access any private (or protected) class fields and methods with reflection. The idea is to switch of the security, get field or method, get reference/invoke method, then switch on the security. Looks like a hack but it works.

A time ago, I wrote a class "ClassRipper" (obviously reinvented a weel). Google search with "ClassRipper" gives a reference to it:

http://www.koders.com/java/fid7E7F02066CC24469AC29D8A3EB9F74D655E53588.aspx

A handy tool for your task. Feel free to use or extend it.

There are 2 similar topics on StackOverflow about this:

Is it possible in Java to access private fields via reflection

How do I read a private field in Java?

UPDATE according to "EDIT1: my question is if there is a solution to access X from ServletA.java WITHOUT creating an instance of A in ServletA.java. The above code is an example to show that i have thought of a method, but it doesnt work."

You cant access to X without creating an instance of A - X doesn't exist (because it's not static). But if your ServletA extends A - then you can use "this" as an instance. ClassRipper handle inheritance. In this case you indirectly have an instance of A ;)

Community
  • 1
  • 1
andrey
  • 842
  • 4
  • 6
  • pretty cool approaches. will look into them as well..thanks. if you find my question useful, please upvote because others may downvote it since they're so impatient :\ – bouncingHippo Jul 23 '12 at 15:58
  • 1
    Don't take downvotes seriously. Some people just doesn't know that this is typical kind of problem for thous who work often with 3rd party libs and without ability to change it design. – andrey Jul 23 '12 at 16:09
0

I'm pretty sure this code won't compile. You can't make a reference to this from a static function.

You could pass an instance of A into ServletA's constructor and remove static from A.getData.

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
-1

A static method does not have a this pointer available, so the code given won't compile. Is there a reason that you need getData() to be static?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268