aspose file tools*
The moose likes Beginning Java and the fly likes problem declaring int x(int y) methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem declaring int x(int y) methods" Watch "problem declaring int x(int y) methods" New topic
Author

problem declaring int x(int y) methods

Sean McManus
Greenhorn

Joined: Aug 03, 2004
Posts: 4
Hello

I'm doing a course in Java and I've got most of my program working, only there's one structure that won't work. I can't even get a listing from a book with a similar structure to work. Here's an example program:

public class Person
{
public static void main(String[] args)
{
int age;

int ageInNYears(int n)
{return age + n ;}
}
}

It always gives me the error message:
; expected
at the line:
int ageInNYears(int n)

I'm actually trying to get a different more complex program to work, but it's falling at the same place in the program - wherever I'm trying to create a method using int. I'm using JCreator. I've got other programs to compile just fine. It's just this construction that isn't working for me so far. I've tried adding in semi-colons all over the place and it's not working - I must be missing something more structural unrelated to this specific line where it fails.

It is completely doing my head in!

If anyone can shed any light on this, it would be most appreciated.

Thank you kindly!


---<br />www.sean.co.uk<br />www.wildmoodswings.co.uk<br />www.JournalismCareers.com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You're trying to declare the new method inside the body of the "main" method -- don't do that. Move your method outside of main, but inside the class.

Your next question will almost undoubtedly have to do with an error message involving the phrase "static context." The solution, at least for now, is to declare your new method static, the same way that "main" is declared static.


[Jess in Action][AskingGoodQuestions]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154



I think you also need to add parentheses
to add n to age before you return anything.
(age + n )
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Elouise Kivineva:

I think you also need to add parentheses
to add n to age before you return anything.


No, that's not necessary.
Sean McManus
Greenhorn

Joined: Aug 03, 2004
Posts: 4
Thanks for your help! Ernest was right - the problem was that I had one method inside another method and declaring it as static and moving it to the right place did fix it.

Much appreciated - I've been able to get both that example program working okay and more importantly the real program I wanted to write working too. I was about to give up on Java altogether even though it's a really small thing, so now my faith is restored and I'll stick at it.

Have a great evening!

Sean
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem declaring int x(int y) methods