Help - Doesn't compile


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help - Doesn't compile

  1. #1
    Rajesh Guest

    Help - Doesn't compile


    Hello There

    Could someone please tell me why the program below doesn't compile. I have
    declared a class called Methods but when I try to use it, I get a compile
    error saying '(' or '[' expected.

    Thanks in advance,

    Rajesh

    ---------------------------------

    class Test {
    class Methods {
    void methodCallOne(int i) {
    System.out.println("Enter methodCallOne...") ;
    System.out.println("i = " + i) ;
    i = i + 5 ;
    System.out.println("i (+5) = " + i) ;
    System.out.println("Leave methodCallOne...") ;
    }
    }
    public static void main(String[] args) {
    int i = 10 ;

    Methods myMethods = new Methods ;
    // myMethods.methodCallOne(i) ;
    System.out.println("i = " + i) ;
    }
    }

    ---------------------------------


  2. #2
    John Butorac Guest

    Re: Help - Doesn't compile

    I'm not quite sure what you are trying to accomplish...but here are a few things that I change to make it compile:

    1. you have two classes...Test and Methods...I believe you only want Methods
    2. thus you have an extra bracket after methodCallOne
    3. when you construct a new Method, you must have the open and close par.

    Copy and Paste what I have below...it should compile.


    // class Test {
    class Methods {

    void methodCallOne(int i) {
    System.out.println("Enter methodCallOne...") ;
    System.out.println("i = " + i) ;
    i = i + 5 ;
    System.out.println("i (+5) = " + i) ;
    System.out.println("Leave methodCallOne...") ;
    }
    // }
    public static void main(String[] args) {
    int i = 10 ;
    Methods myMethods = new Methods();
    myMethods.methodCallOne(i) ;
    System.out.println("i = " + i) ;
    }
    }



    "Rajesh" <jstudent01@hotmail.com> wrote in message news:3a6ca187$1@news.devx.com...
    >
    > Hello There
    >
    > Could someone please tell me why the program below doesn't compile. I have
    > declared a class called Methods but when I try to use it, I get a compile
    > error saying '(' or '[' expected.
    >
    > Thanks in advance,
    >
    > Rajesh
    >
    > ---------------------------------
    >
    > class Test {
    > class Methods {
    > void methodCallOne(int i) {
    > System.out.println("Enter methodCallOne...") ;
    > System.out.println("i = " + i) ;
    > i = i + 5 ;
    > System.out.println("i (+5) = " + i) ;
    > System.out.println("Leave methodCallOne...") ;
    > }
    > }
    > public static void main(String[] args) {
    > int i = 10 ;
    >
    > Methods myMethods = new Methods ;
    > // myMethods.methodCallOne(i) ;
    > System.out.println("i = " + i) ;
    > }
    > }
    >
    > ---------------------------------
    >



  3. #3
    Shayne Guest

    Re: Help - Doesn't compile


    Rajesh,

    Methods myMethods = new Methods ;

    should be

    Methods myMethods = new Methods();

    Good luck.

    "Rajesh" <jstudent01@hotmail.com> wrote:
    >
    >Hello There
    >
    >Could someone please tell me why the program below doesn't compile. I have
    >declared a class called Methods but when I try to use it, I get a compile
    >error saying '(' or '[' expected.
    >
    >Thanks in advance,
    >
    >Rajesh
    >
    >---------------------------------
    >
    >class Test {
    > class Methods {
    > void methodCallOne(int i) {
    > System.out.println("Enter methodCallOne...") ;
    > System.out.println("i = " + i) ;
    > i = i + 5 ;
    > System.out.println("i (+5) = " + i) ;
    > System.out.println("Leave methodCallOne...") ;
    > }
    > }
    > public static void main(String[] args) {
    > int i = 10 ;
    >
    > Methods myMethods = new Methods ;
    >// myMethods.methodCallOne(i) ;
    > System.out.println("i = " + i) ;
    > }
    >}
    >
    >---------------------------------
    >



  4. #4
    ric Guest

    Re: Help - Doesn't compile


    "Rajesh" <jstudent01@hotmail.com> wrote:
    >
    >Hello There
    >
    >Could someone please tell me why the program below doesn't compile. I have
    >declared a class called Methods but when I try to use it, I get a compile
    >error saying '(' or '[' expected.
    >
    >Thanks in advance,
    >
    >Rajesh
    >
    >---------------------------------
    >
    >class Test {
    > class Methods {
    > void methodCallOne(int i) {
    > System.out.println("Enter methodCallOne...") ;
    > System.out.println("i = " + i) ;
    > i = i + 5 ;
    > System.out.println("i (+5) = " + i) ;
    > System.out.println("Leave methodCallOne...") ;
    > }
    > }
    > public static void main(String[] args) {
    > int i = 10 ;
    >
    > Methods myMethods = new Methods ;
    >// myMethods.methodCallOne(i) ;
    > System.out.println("i = " + i) ;
    > }
    >}
    >
    >---------------------------------
    >

    Hello Rajesh,
    Methods is an inner class of Test.
    Methods is not static.
    You can't reference a non-static member from a static method.
    You have to create an instance of Methods with an enclosing instance of Test.
    Use the following statement to create it.
    Methods myMethods = new Test().new Methods();
    Let me know if this helps. Ric



  5. #5
    bhupesh Guest

    Re: Help - Doesn't compile


    "Rajesh" <jstudent01@hotmail.com> wrote:
    >
    >Hello There
    >
    >Could someone please tell me why the program below doesn't compile. I have
    >declared a class called Methods but when I try to use it, I get a compile
    >error saying '(' or '[' expected.
    >
    >Thanks in advance,
    >
    >Rajesh
    >
    >---------------------------------
    >
    >class Test {
    > class Methods {
    > void methodCallOne(int i) {
    > System.out.println("Enter methodCallOne...") ;
    > System.out.println("i = " + i) ;
    > i = i + 5 ;
    > System.out.println("i (+5) = " + i) ;
    > System.out.println("Leave methodCallOne...") ;
    > }
    > }
    > public static void main(String[] args) {
    > int i = 10 ;
    >
    > Methods myMethods = new Methods ;
    >// myMethods.methodCallOne(i) ;
    > System.out.println("i = " + i) ;
    > }
    >}
    >
    >---------------------------------
    >try this code

    Test t =new Test();
    Methods m = t.new Methods();
    m.methodCallOne(i);



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center