David Hermanto
David Hermanto

Reputation: 23

A way to get a variable in a method from a constructor in the same class?

I'm very new at code, and I can't find an explanation or solution that I can understand even though I'm pretty sure this is simple. I hope I got the terms right too. What I'm confused about is:

public class Item{

   public Item(int number1, int number2, int number3){
      //Nothing really significant in here, just some mathematics
   }

   public int getNumber(){
      //I want to get the values of number 1-3 here, without parameters 
      //or any data structures if possible.^
   }

}

If anyone could please explain it to me, I would be grateful. I would've looked up more, but I spent already half a day around these kind of problems and I'm not exactly experienced.

Thank you!

Upvotes: 0

Views: 272

Answers (5)

belka
belka

Reputation: 1530

If you want to be able to retrieve a value at some point, you have to store it somewhere. This is done at the initialization phase when you create a new object with the constructor. When you call a constructor, you need to store the values you need when building your object. By doing that, your object keeps the needed values number1, number2, number3. Note that if you need to store an indefinite number of numbers that are not semantically defined (eg. you only store numbers that are not an area, a price, a quantity defined by a given name) then you should maybe store them inside an array.

public class Item {
   private int number1; // internal attributes
   private int number2; // are initialized
   private int number3; // in the constructor

   public Item(int number1, int number2, int number3) { // constructor
        this.number1 = number1;
        this.number2 = number2; // setup internal attributes
        this.number3 = number3;
   } 
}

Then, when calling a getter, you may fetch the stored values. Your class has now 3 new functions.

public class Item {
   private final int number1;
   private final int number2;
   private final int number3;

   public Item(int number1, int number2, int number3){
        this.number1 = number1;
        this.number2 = number2;
        this.number3 = number3;
   }

   // add getter methods that only return internal attributes
   // values, so that it would be impossible to modify them
   // from the outside

   public int getNumber1() {
      return number1; // equivalent of return this.number1;
   }

   public int getNumber2() {
      return number2;
   }

   public int getNumber3() {
      return number3;
   }

}

Hope this solves your problem.

Upvotes: 1

MD RAFIUDDIN
MD RAFIUDDIN

Reputation: 1

check the snippet code :-

 import java.util.*;


 public class MyClass

 {
 public List<Integer> setNumberList = new ArrayList<Integer>();


 public void setNumbers(int number1,int number2,int number3)
  {

   setNumberList.add(number1);
   setNumberList.add(number2);
   setNumberList.add(number3);


  }


  public List<Integer> getNumbers()
  {

  return setNumberList;

  }


  public static void main(String args[])
  {

   MyClass obj = new MyClass();

   obj.setNumbers(9,2,3);
   List<Integer> getyourNumbers= obj.getNumbers();

   //the whole data is save in your list now you can get data by iterating it as you 
   want
   System.out.print(getyourNumbers);

  }

  }

Upvotes: 0

robertobatts
robertobatts

Reputation: 967

When you call Item(...) you need to save the parameters in the class properties, so that you can access them later when you call getNumber()

public class Item{

   private int number1;
   private int number2;
   private int number3;

   public Item(int number1, int number2, int number3){
      this.number1 = number1;
      this.number1 = number2;
      this.number1 = number3;
      //Nothing really significant in here, just some mathematics
   }

   public int getNumber(){
      //here you can access to this.number1, this.number2, this.number3
   }

}

Upvotes: 0

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

In constructor you can initialize class's variables. These variables belong to the class' instance, so the're available in the class' method. Each object that you create with new Item(1,2,4) will have it's own set of these fields.

To get each variable, it's better to use getters.

public class Item {

    private final int number1;
    private final int number2;
    private final int number3;

    // Method with same name as class called Constructor
    public Item(int number1, int number2, int number3){
        this.number1 = number1;
        this.number2 = number2;
        this.number3 = number3;
    }

    public int getNumber1() {
        return number1;
    }

    public int getNumber2() {
        return number2;
    }

    public int getNumber3() {
        return number3;
    }
}

Upvotes: 1

Michael Michailidis
Michael Michailidis

Reputation: 1052

You cannot really get 3 integers in 1 integer without any data structures. Think it in a way you wanna fit 3 sim cards in your phone with 1 slot.

also the function is named getNumber() which raises the question which number? Same as the others said you will need to store your parameters in your class in some form so you will reuse them later

So you should use something like int[] or List<Integer> or even better a custom class named for example Numbers with methods getNumber1 getNumber2 getNumber3.

Hope that helped!

Upvotes: 0

Related Questions