Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Method problem

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Method problem

    Hi again
    Ok so this time I was fiddling around with methods, and trying to figure them out, and once I got them working I got this error - illegal start of expression.
    public class methodTest1a{
        public static void main(String[]args){
     
        Method1();
        Method2();
     
                                  public static void Method1(){
            System.out.println("method 1");
        }
            public static void Method2(){
                System.out.println("method 2");
            }
            }
    }
    the error appears on the line which I spaced out, and I was wondering what the problem is. When I go shift-F6, the file runs fine, however I want to know what the error is.
    thanks,
    Skeptile


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Method problem

    Your squiggly braces are in disarray... always keep them nice and lined up so you can see where everything ends and begins. Something is wrong with your braces. They should look like this and your app will run fine.

    public class InClass
    {
    public static void main(String[] args)
    {
    method1();
    method2();
    }

    public static void method1()
    {
    System.out.println( "hello" );
    }

    public static void method2()
    {
    System.out.println( "hello" );
    }

    }

    --- Update ---

    I believe your methods are inside of your main method because of the way your squiggly braces are set up. You should never have methods in your main method. That's why you're getting a compile error.

  3. The Following User Says Thank You to bdennin For This Useful Post:

    skeptile2 (March 6th, 2013)

  4. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Method problem

    You are not allowed to define a method within another method. Separate your method definitions out of your main method.

    Things you should know about Methods:

    Method:
    -A method is invoked by a method call - Name(args)
    -Most methods are invoked through a reference call to a object
    -Method args can be: variables, constants, expressions
    -Must be defined in a class definition
    -Format: return-type method-name(paramter list){declarations and statements}
    -At most can return 1 value
    -A method cannot be defined inside another method.
    -Method control is given back to caller when } is reached or return;

    Variables:
    -Variables in method definitions are local to that method only
    -Instance variables should be only used if they are required in more than
    one method.

    Method Calls:
    -thisMethod();
    -object.method(); //object refers to class
    -Class.method(); //static methods

  5. #4
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Method problem

    Quote Originally Posted by skeptile2 View Post
    Hi again
    Ok so this time I was fiddling around with methods, and trying to figure them out, and once I got them working I got this error - illegal start of expression.

    the error appears on the line which I spaced out, and I was wondering what the problem is. When I go shift-F6, the file runs fine, however I want to know what the error is.
    thanks,
    Skeptile
    you call the method like this in main method...

     methodTest1a m;
            m = new methodTest1a();
            m.Method1();
            m.Method2();
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. Calculate method problem.....
    By DavidXCode in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2012, 10:04 PM
  2. [SOLVED] Problem with split() method
    By andreas90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 21st, 2012, 06:15 PM
  3. Method access problem
    By hello_world in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2011, 03:39 PM
  4. dynamic method problem...
    By Ranger-Man in forum Object Oriented Programming
    Replies: 8
    Last Post: September 7th, 2009, 04:22 PM
  5. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM