Results 1 to 5 of 5
Thread: static method
- 05-22-2012, 11:31 PM #1
Member
- Join Date
- May 2012
- Posts
- 1
- Rep Power
- 0
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.
- 05-23-2012, 01:56 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
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)
- 05-23-2012, 03:23 AM #3
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.
- 05-23-2012, 07:21 AM #4
Re: static method
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-23-2012, 10:09 AM #5
Member
- Join Date
- May 2012
- Posts
- 18
- Rep Power
- 0
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 10:12 AM.
Similar Threads
-
Error in Code: Non-static method cannot be referenced from a static context
By oaklandsbest in forum New To JavaReplies: 9Last Post: 06-10-2011, 12:40 AM -
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks