Help a java noob :(

Soldato
Joined
28 Oct 2006
Posts
12,457
Location
Sufferlandria
My program is in the form:

main
{
int variable1 =0;
int variable2 =0;
int variable3 =0;

public void method1()
{
*stuff*
}

public void method2()
{
*stuff*
}

public void method3()
{
*stuff*
}
}

how do i make it so that if i cange a variable inside one method, other methods will use the updated value rather than the default, 0?
 
Code:
class Main
{
    int variable1 =0;
    int variable2 =0;
    int variable3 =0;

    public static void main(String[] args)
    {
        method1();
    }

    public void method1()
    {
        variable1 = 1;
        method2();
    }

    public void method2()
    {
        Sys.out.print(variable1.toString()); // prints '1'
    }
}

can also use 'this.variable1' or 'this.method1()' etc to avoid ambiguity.
 
toString() is not actually needed since it is called implicitly, but I guess you are just putting it for clarity.
 
Accessor methods are hardly necessary for something small like that. I agree you should normally have accessors/mutators if you are having multiple classes for proper encapsulation. Right now all you would do is increase the call stack unnecessarily :p

Personally I would only provide accessor/mutator methods if they are a sensible and useful part of the class's interface.
 
Last edited:
I think this is what you want and demonstrates how to return values:

Code:
class test
{

	public static void main(String[] args)
    {
	test me = new test();
        me.method1();
    }

    public void method1()
    {
        int variable1 = 0;
        int variable2 = 0;
        int variable3 = 0;
        variable1 = method2(variable1);
        method3(variable1);
    }
    
    public int method2(int variable1)
    {
    	variable1 = 1;
    	return variable1;
    }

    public void method3(int variable1)
    {
        System.out.println(variable1);
    }
}

Although find a good Java tutorial, you shouldn't really have to ask how to do stuff like this a tutorial should cover it.
 
Una said:
Accessor methods are hardly necessary for something small like that. I agree you should normally have accessors/mutators if you are having multiple classes for proper encapsulation. Right now all you would do is increase the call stack unnecessarily :p

Personally I would only provide accessor/mutator methods if they are a sensible and useful part of the class's interface.

Its good practice to use accessor / mutator methods early, especially when building your skills.

Also, when you build classes, make sure you specify it with a capital. :)

I'd also call all the methods from the main method. It can avoid confusion later one.

Code:
public class NumberDemo
{
	private int nOne, nTwo, nThree;
	
	public NumberDemo()
	{
		setNumberOne(5);
		System.out.println( getOne() );
	}
	
	public void setNumberOne(int value)
	{
		nOne = value;
	}
	
	public int getOne()
	{
		return nOne;
	}
	
	public int getTwo()
	{
		return nTwo;
	}
	
	public int getThree()
	{
		return nThree;
	}
	
	public static void main(String[] args)
	{
		NumberDemo testObjeect = new NumberDemo();
	}

}
 
I'm a little confused, are you asking how to access member variables? :confused:

If you mean within the same class, use "this.variable", else use get methods as the guys before me have shown
 
accessors can be code smells, particularly for non-validated/challenged accessing. Infact, they are code smells everywhere, unless the accessor is performing one of the following:

a) validating
b) restricting
c) logging/maintaining
d) part of the interface
e) processing/calculating/converting
 
Back
Top Bottom