Results 1 to 3 of 3
  1. #1
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Question Having trouble with calling methods.

    I am trying to figure out the output of this code:
    Java Code:
    class Confusing {
    		int x = 3; 
    		int y = 5; 
    	 public Confusing() 
    		{ 
    			x = 3; 
    			y = 5; 
    		} 
    		static void first( int a, int b) 
    		{ 
    			int temp = x; 
    			x = y; 
    			y = temp; 
    		}//end method first 
    		static void second( int x, int y) 
    		{ 
    			first( x, y); 
    			x = y + 1; 
    		}//end method second 
    		public void doIt() 
    		{ 
    			int x = 1; 
    			int y = 3; 
    			first( x,y); 
    			second( y,x); 
    			System.out.println("Answer is: " + this.x); 
    		}//end method doIt() 
    }
    The method doIt() that Confusing is calling is from this code:
    Java Code:
    class ClassADriver
    	{
    		public void doIt()
    		{
    			int value = 7;
    			ClassA instance1 = new ClassA( value);
    			instance1.x = value -1;
    			instance1.changeValue(2);
    			System.out.println("value is: " + instance1.x);
    		}
    }
    Which is also calling from this class
    Java Code:
    class ClassA {
    
    	private int x;
    	public ClassA()
    	{
    		x = 1;
    	}
    
    	public ClassA( int x)
    	{
    		this.x = x+1;
    	}
    	public void changeValue(int val)
    	{
    		x = x + val;
    	}
    }

    When I attempted at compiling the first two of these codes, I got the following errors in my Terminal:

    Confusing.java:12: non-static variable x cannot be referenced from a static context
    int temp = x;
    ^
    Confusing.java:13: non-static variable x cannot be referenced from a static context
    x = y;
    ^
    Confusing.java:13: non-static variable y cannot be referenced from a static context
    x = y;
    ^
    Confusing.java:14: non-static variable y cannot be referenced from a static context
    y = temp;
    ^
    4 errors


    ClassADriver.java:25: x has private access in ClassA
    instance1.x = value -1;
    ^
    ClassADriver.java:27: x has private access in ClassA
    System.out.println("value is: " + instance1.x);
    ^
    2 errors


    ClassA compiled just fine. Now, without changing the code (I'm not supposed to change the code, this is part of an assignment) I am just confirming whether or not for the errors I received in Confusing class are supposed to occur and I am hoping (since I am terrible at Java programming) for a simple explanation from anybody willing to help!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Having trouble with calling methods.

    The method: first is static. That means it can NOT see any non-static class variables without a reference to an instance of the class.

    Why is the first method static? Can you make it non-static?
    Or change x and y to be static.

    What is the purpose of the code? There is a design problem with using static and non-static variables and methods like it does.

  3. #3
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: Having trouble with calling methods.

    Well this is part of an exam and more than likely my professor purposely had done this in order to get us thinking about the actual code itself. It's a multiple choice question but I didn't want to post simply the actual question I need to answer because I wanted to see what the code was generating and if what I did was necessarily wrong. So that's essentially the "purpose" of the code and the reason behind why there are static and non-static variables. But I s'pose I did everything correctly then so the answer must be "none of the above" so thank you for confirming that for me (:

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Trouble with calling a method
    By jrJava in forum New To Java
    Replies: 6
    Last Post: 01-17-2011, 09:51 PM
  3. Trouble working with/calling Objects! Any Help?
    By ramathews in forum New To Java
    Replies: 4
    Last Post: 03-24-2010, 02:50 PM
  4. Trouble will calling a method
    By jonsamwell in forum New To Java
    Replies: 9
    Last Post: 08-22-2008, 10:16 PM
  5. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •