0

I know pointers don't exist the way they do in C, but I was wondering if something like this would be possible:

I have 5 global variables, r1 r2 r3 r4 r5, each initially initialized to null

I want to (1) find the variable that hasn't already been used, (2) assign something to it, and then (3) manipulate that variable later in the program. However, I don't know the status of the variables until code execution. To make up for this, I now have a bunch of if-else statements:

if(r1==null)
    r1 = [something]    
else if(r2==null)
    r2 = [something]
else if(r3==null)
    r3 = [something]
else if(r4==null)
    r4 = [something]
else if(r5==null)
    r5 = [something]

So far so good. But the problem is, I want to take the variable that was modified in the above code and use/modify that variable later in the program.

So say if r1 and r2 were NOT null, and r3 was null, r3 = [something]. I want to modify r3 later on in the program. In C/C++, I'm thinking I could have set up a pointer to r3 in the if-else statement. Then I could just modify r3 through that pointer later in the program.

Is this possible in Java? Thanks!

EDIT: forgot to mention, r1-r5 are Strings.

3
  • It could be done using Java's Field type in the reflection APIs. Commented Dec 5, 2012 at 3:59
  • Even if r1-r5 are just Strings? Commented Dec 5, 2012 at 4:00
  • 2
    By the way, this smells like poor design. Are you sure you can't make your code more object oriented? Maybe you could wrap your string in an object that maps what the strings in fact represent? Commented Dec 5, 2012 at 4:06

6 Answers 6

4

Java is an Object oriented language, and Objects are passed by reference. So if your variables r1 - r5 are instances of a particular Class passing them around will be the same as passing objects around.

So modifying the object in a separate function or method will update the original objects also.

Keep a good Java book under your pillow; Dietel and Dietel for starters, Java LRM for the graduated.

Sign up to request clarification or add additional context in comments.

Comments

2

It sounds like you want to use an array. Once you figure out which element hasn't already been used, just save the index and work with that.

Comments

1

For something this simple, you could use an array. You can keep an array index as pointer to the variable you want.

String[] r = new String[5]; // all nulls

and so:

private int findFirstNull(String[] array) {
  for(int i=0; i<r.length; i++)
    if(r[i] == null)
      return i;
  return -1;
}

Comments

0

Java's reflection APIs allow references to member variables via the Field type.

In a nutshell, you can store a reference to the desired global variable in a Field object and query/update it on demand. For example, supposing that the global variables have type String:

// static String MyClass r1 = null;
Field theGlobal = MyClass.class.getField("r1");
String s = (String) theGlobal.get(MyClass.class);
if (s == null) {
  theGlobal.set(MyClass.class, "Hello, Reflection!");
  // MyClass.r1 == "Hello, Reflection!"
}

However, the need to do so definitely seems like poor design. It is likely that your global variables could be better represented by some other member variable type(s).

Comments

0

Java passes its object by value. While you can modify the state of the object passes to a method and those modification is preserved outside the method, assign the object with another instance won't.

For example, doing something like this is preserved:

point.setX(10);
point.setY(100);

while something like this won't:

point = new Point(10,100);

And as you stated that you are using Strings, String in java is immutable, thus modifying its state is not possible.

Like some of the other answers suggested, using Array of String in this situation is an excellent alternative. Since its a global variable, you could use something like this:

public static final String[] r = new String[5];

with that, changing one of its element like

r[0] = "Some String";

will be preserved globally.

Comments

-1

I think you can have a separate global variable to know which one was modified (I think you just need the reference of that string) and use like:

if(r1==null)
    globalR = r1 = [something]    
else if(r2==null)
    globalR = r2 = [something]
else if(r3==null)
    globalR = r3 = [something]
else if(r4==null)
    globalR = r4 = [something]
else if(r5==null)
    globalR = r5 = [something]

then you can use globalR to handle that string

2 Comments

If for handling you mean reading, then OK, but you cannot do anything else. Strings are immutable objects in Java.
Yes, I meant reading and then do what you want for that string, ofcourse there'll be a new object if you change string as they are immutable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.