Results 1 to 5 of 5
  1. #1
    carrot_spy is offline Member
    Join Date
    Oct 2012
    Location
    Washington State
    Posts
    2
    Rep Power
    0

    Default Calling up getter methods?

    Hey all, any help would be greatly appreciated

    So I have two classes, one called Point, and the other is called Circle. In class Point, there is a method that takes parameters to create a point in an x,y plane, but does not return them. Later in the program are two getter methods that return the x and y values. My problem is that when I call on the getter methods in class Circle, they only return 0,0. Does calling on a getter method not automatically run the method it gets it's data from?

  2. #2
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Calling up getter methods?

    its hard to help you with no code example. Although if you do post one try to make a smaller example of the classes your talking about and show us how you code the getter. It should get the right values. Here is how a typical getter would go.

    Java Code:
    public class Test{
    	
    	public static void main(String[] arg){
    		NewClass test = new NewClass(5, 8);
    		
    		System.out.println(test.getX() + " " + test.getY());
    		
    		
    		//or
    		for(int i: test.getXandY())
    			System.out.print(i+" ");
    		
    	}
    	
    	
    }
    
    class NewClass{
    	
    	int x;
    	int y;
    	
    	public NewClass(int x, int y){
    		this.x = x;
    		this.y = y;
    	}
    	
    	public int getY(){
    		return y;
    	}
    	
    	public int getX(){
    		return x;
    	}
    	
    	public int[] getXandY(){
    		int[] ret = {x,y};
    		return ret;
    	}
    	
    }
    The int[] return might be consider bad practice but should work.
    Last edited by killutch; 10-22-2012 at 12:28 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: Calling up getter methods?

    Quote Originally Posted by killutch View Post
    The int[] return might be consider bad practice but should work.
    If you are referring to creating an array to return 2 values because a method can only return a single value, then yes this is not a good idea.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,192
    Rep Power
    19

    Default Re: Calling up getter methods?

    Quote Originally Posted by carrot_spy View Post
    Hey all, any help would be greatly appreciated
    Seeing as you didn't bother to return to the first thread you started, I find that difficult to believe.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Calling up getter methods?

    Quote Originally Posted by Junky View Post
    If you are referring to creating an array to return 2 values because a method can only return a single value, then yes this is not a good idea.
    I considered talking about making a class called Location if he even more data he wanted to package as one but I figured that might be over board.

Similar Threads

  1. Calling methods
    By Alkor in forum New To Java
    Replies: 15
    Last Post: 02-25-2012, 02:33 PM
  2. help with getter and setter methods of this question.
    By luciawashaya in forum New To Java
    Replies: 6
    Last Post: 11-01-2011, 11:28 AM
  3. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 09:12 PM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 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
  •