Results 1 to 5 of 5

Thread: static method

  1. #1
    Katiec29 is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default static method

    I have a static method with argument number,
    public static int newNumber(number)
    {
    callatz sequence etc

    i need to access the value of the parameter(number) from a non static method just so confused on how to do this,

    public void print()

    i need to access the values so i can:

    Systme.out.print(number).

    anybody have any ideas.

    i would rather a point in the right direction.

    thankyou ... Shift+R improves the quality of this image. CTRL+F5 reloads the whole page.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: static method

    static/class variables are accessed by the class name followed by the variable name. See Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

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

    Default Re: static method

    Static/non-static aside, parameters to a method are local to that method. You cannot access them outside that method. Time to rethink your design.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: static method

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

  5. #5
    Art Vandelay is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: static method

    Hi Katie,

    I agree it can get confusing. I also agree with previous posters it might be a good idea to rethink the design. I put together a little example based on what you need as a starting point:

    Java Code:
    class PrintNewNumber {
    
    	private int number;
    
    	public PrintNewNumber(int number1){
    
    		this.number = number1;
    		System.out.println(number);
    		PrintNumberAgainButAcceptAnyNumber(number);
    		PrintNumberAgainButAcceptAnyNumber(20);
    		PrintTheOriginalAgain();
    
    
    	}
    
    	public static void main (String args[]) {
    		new PrintNewNumber(13);
    	}
    
    	static void PrintNumberAgainButAcceptAnyNumber(int number2) {
    		System.out.println(number2);
    	}
    
    	private void PrintTheOriginalAgain( ) {
    		System.out.println(number);
    	}
    
    }
    Last edited by Art Vandelay; 05-23-2012 at 11:12 AM.

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  2. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  3. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  4. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 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
  •