-
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) ;
}
}
---------------------------------
-
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) ;
> }
> }
>
> ---------------------------------
>
-
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) ;
> }
>}
>
>---------------------------------
>
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks