-2

As the title explains I am hoping to be able to set an Instance Variable using data produced by a calculation contained within a Method.

I produced an example code to demonstrate what I'm trying to do, as well as a solution I pieced together after scouring the internet. Obviously though, I failed to successfully replicate what others had done.

Thanks in advance for your help.

This code is intended to set the instance variable of "Max" to the value of "6" using the "myMethod" method, and then use the "printMethod" to print the value of "Max".

public class Main {
//Main class, Of Coruse. 
    private int Max;
//The value I wish to be changed by the method. 

    public int getMax() {
        return Max; 
    }//The process which is supposed to get the value from the method. 

    public static void main(String[] args) {
        Main Max = new Main();
        {
            Max.printMax();
        }
    }//The main method, and non-static reference for printMax. 

    public void myMethod() {//Method for assigning the value of "Max" 
        int Lee = 6;
        this.Max = Lee;
    } 

    public void printMax() {//Method for setting, and printing the value of "Max" 
        Main max = new Main();
        int variable = max.getMax();

        System.out.println(variable);
    } 
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
DeezNuts
  • 11
  • 1
  • 3
  • I recommend that you try to improve the formatting of your code that you post in here and your code in general. Good formatting including using an indentation style that is uniform and consistent will help others (**us**!) to better understand your code, and more importantly, it will help **you** to better understand your code and thus fix your own bugs. Also it shows that you're willing to put in extra effort to make it easier for the volunteers here to help you, and that effort is **much** appreciated. – Hovercraft Full Of Eels Aug 11 '16 at 01:13
  • 2
    I don't understand your question. Please clarify. Why are you creating a new `Main` object within another `Main` object's `printMax` method? What is the purpose of `myMethod`? You're not currently using it. – Sotirios Delimanolis Aug 11 '16 at 01:14
  • @Hovercraft Full Of Eels, Ah yes I apologise for the code formatting It was basically the result of me being stressed trying to figure out what I was doing wrong, as well as the fact that I was trying to piece together bits of code I'd found on the net in an attempt to replicate other peoples success. This isn't the code I'm actually using for my program, this was just a mockup of a potential solution I made hastily on my program testbench. – DeezNuts Aug 11 '16 at 02:59
  • @Sotirios Delimanolis, The question was that I wanted to use the method to be able to set the value of a variable that exists outside the method. Of course due to scope issues I couldn't do that, however it was necessary since the method contained (in my actual code) a calculation that was necessary to another class. As for the usage of different variables and methods in this code, I refer to my above comment in saying that this was really just a quick mock-up made from code scraps, an example to demonstrate what I needed. – DeezNuts Aug 11 '16 at 03:02
  • can you please elaborate what exactly u r trying to do? this code...have any error.? its not clear what actually you are trying...or what you wanted – Lijo Aug 11 '16 at 05:22
  • @Lijo. Please refer to the title of this question, as well as the first line of the description, and the first line of the second comment in this particular row of responses. I don't mean to be rude, but I have specifically mentioned what my intentions are, to be able to set the value of a variable outside of a method, using data contained within a method. – DeezNuts Aug 11 '16 at 14:25

1 Answers1

1

I think you have some misunderstandings of how instances of a class work. I recommend you to learn the basics of OOP first.

Anyway, although you didn't tell me what should be the expected result, I guess that you want to print 6. So here's the solution:

private int Max;

public int getMax() {
    return Max;
}

public static void main(String[] args) {
    Main Max = new Main();
    Max.printMax();
}

public void myMethod() {
    int Lee = 6;
    this.Max = Lee;
}

public void printMax() {
    this.myMethod();
    int variable = this.getMax();
    System.out.println(variable);
}

Let me explain what I have changed.

  • In your main method, there is no need for the {} after new Main(), so I deleted them.
  • In printMax, there is no need to create a Main instance again because this already is one.
  • The reason why you failed to print 6 was because the variable max was never changed. Why? Because you just declared a method that changes max, but that method (myMethod) is not called! So I just added a line to call myMethod.
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you very much for your answer, I can completely understand my failings with this code now, thankfully this code is only an example. I suppose in trying so hard to find a solution I overlooked some painfully basic concepts, I suppose that is also the result of trying to piece together scaps of code I found online as well. I really only started programming with Java a few days ago so I still have quite a few things yet to learn about the language, and about concepts like OOP also. Thanks once again, your help has allowed me to make functional some vital parts of my actual program. – DeezNuts Aug 11 '16 at 03:07
  • If you think my answer answers your question, please consider accepting it by clicking that checkmark! @DanielHill – Sweeper Aug 11 '16 at 03:08