0

I am new to java. I want to know how I can use the variable in the whole java class and keeping the value of it. Suppose in a method I fill the variable with a value and I want to use the value of it in another method.

public class Test {
    public String id;

    public void includeClient() {
        String id = baseClass.createCleint();
    }

    public void removeClient() {
        System.out.println(id);
    }
}

in second function it returns null. Any idea?

2
  • ya I just forgot to write String when I wanted to ask my question. Commented Aug 6, 2014 at 15:17
  • Please please please fix the spelling error in method name createCleint() My eyes hurt! Commented Aug 6, 2014 at 15:55

5 Answers 5

4

In the method includeClient() you assigned the value to a local variable having the same name as the instance variable. The other method (which, BTW, can't have the same signature as the first method) sees the instance variable, which is still null.

Change it to :

public void includeClient() {
    id = baseClass.createCleint();
}
Sign up to request clarification or add additional context in comments.

2 Comments

@user3409650 make sure that baseClass.createCleint() doesn't return null.
@user3409650 It's hard to say when the code you posted is incomplete and doesn't even compile (both methods have the exact same name, and baseClass is not defined anywhere).
1

Remove String from String id = baseClass.createCleint(); as it is local variable for the method and will be assiged the value when you call method and garbage collected after the execution of method and not accessible outside the method.

In short you are not assigning value to the variable declared at class level but you are creating another one.You better use Constructor to perform initialization.

Secondly you have declare public void includeClient() { twice I bet it's typo.

public class Test {
    public String id;

    public void includeClient() {
        id = baseClass.createCleint();
    }
}

Comments

1
    public class Test {
        public String id;

        public void includeClient() {
            String id2 = baseClass.createCleint();
            System.out.println(id2);
            id = id2;
         }

        public void includeClient2() {
            System.out.println(id);
        }
    }

Use this to understand and test.

List of changes made -

  1. Changed second method name to make it unique
  2. Assigned return value to local variable named different than class member variable.
  3. First print return value to check what it is returning
  4. Assign local value to member variable.

Note: You still need to read a lot about java. Just keep practicing.

Comments

0

Replace:

String id = baseClass.createCleint();

by

this.id = baseClass.createCleint();

or

id = baseClass.createCleint();

Comments

0

An important thing to note about this is that there's two ways to share a variable like this - you can have each object of the class have its own copy of the variable, or you can have every object of the class share the same one variable. The keyword static lets you do the latter:

class Test {
  public String message;
}

class TestStatic {
  public static String message;
}

If you have instances of the first class, they behave like each instance has its own message:

Test testA = new Test();
Test testB = new Test();

testA.message = "Hello!";
testB.message = "Greetings!";

System.out.println(testA.message);
System.out.println(testB.message);

But with the second class, what happens is that the class itself has a message and all instances of the class refer to it, so there's only one message that's shared between all of them:

TestStatic testA = new TestStatic();
TestStatic testB = new TestStatic();

TestStatic.message = "Hello!";

System.out.println(testA.message);
System.out.println(testB.message);

Note that we didn't set message using either testA.message or testB.message as above - we set it using the class with TestStatic.message. This is because message doesn't really belong to either testA or testB, it belongs to the class and testA and testB simple have access to their class's members.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.