Call method(s) within the same class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Call method(s) within the same class

  1. #1
    Join Date
    May 2011
    Posts
    41

    Call method(s) within the same class

    I am new to java. I am trying to create 3 methods within a class and then call them in that same class. I am not creating or calling correctly. Please help. Here's my code:

    Code:
    import java.util.*;
    
    public class Ch4_PrExercise2
    {
    	public static void main(String [] args)
    	{
    		Scanner console = new Scanner(System.in);
    		
    		int num1, num2, num3;
    		
    		System.out.println("Please input 3 numbers separated by spaces: ");
    		System.out.println();
    		
    		num1 = console.nextInt();
    		num2 = console.nextInt();
    		num3 = console.nextInt();
    		
    	
    		
    			public int largest()
    			{
    				if(num1 > num2 && num1 > num3)			
    					return num1;			
    				else if(num2 > num1 && num2 > num3)			
    					return num2;				
    				else if(num3 > num1 && num3 > num2)
    					return num3;
    			}						
    			
    	
    		
    			public int smallest()
    			{
    				if(num1 < num2 && num1 < num3)
    					return num1;
    				else if(num2 < num1 && num2 < num3)
    					return num2;
    				else if(num3 < num1 && num3 < num2)
    					return num3;
    		   }
    			
    			 public int middle()
    			 {
    		
    				if((num1 < num2 && num1 >num3) || (num1 > num2 && num1 < num3))
    					return num1;
    				else if((num2 > num1 && num2 < num3) || (num2 < num1 && num2 >num3))
    					return num2;
    				else if((num3 > num2 && num3 < num1) || (num3 < num2 && num3 > num1))
    					return num3;
    			 }
    				
    		
    		System.out.println("Your numbers in nondescending order are: " + " " + smallest() + " " + middle() + " " largest());		
    		
    		
    	}
    }

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Call method(s) within the same class

    Java doesn't allow you to declare methods within another method. So move the three methods outside of main.

    Now if you call these methods from main you must, either declare them static (just like main), or
    instantiate Ch4_PrExercise2 using new and then use the reference to call the methods like,

    Ch4_PrExercise2 e = new Ch4_PrExercise2(); // first thing in main
    //
    e.largest() // later in main

    Finally you must either move

    int num1, num2, num3;

    outside of main, or pass these variables as parameters to the methods, or both.
    Last edited by nuzzle; September 18th, 2011 at 11:54 PM.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center