This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Another Simple Java Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Another Simple Java Question" Watch "Another Simple Java Question" New topic
Author

Another Simple Java Question

Kareem Adams
Greenhorn

Joined: Sep 22, 2007
Posts: 6
Please see the 2 similar codes below:

Code 1 :

Code 2 :


Note: when I place System.out.println somewhere else, I get errors.

illegal start of expression


I just need an understanding of why is that.

Besides, the println() is not in the sum().


Thanks.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
It's nothing to do with where the System.out.println is. Your sum method is defined inside your main method. You can't do that in Java.


Joanne
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Kareem Adams wrote:Note: when I place System.out.println somewhere else, I get errors.

It's got nothing to do with that. In Code 2 you have a method inside a method.

My advice: Indent your code properly. It will save a lot of these avoidable errors.

Winston

PS: Too slow.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Scratch that. Winston and Joanne already said it.
Kareem Adams
Greenhorn

Joined: Sep 22, 2007
Posts: 6
Thank you Joanne.

That is what I want to understand.

Your sum method is defined inside your main method. You can't do that in Java.



1 - One can't define a method inside main method. ~ Lesson learned

2 - One can't define a method inside another method. ~ Lesson learned - hope this is right as well.


These are some of the little things that keeps giving me hard times with Java. I'll sure come with more.


Thank you all for your assistance.


Y'all appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Kareem Adams wrote:1 - One can't define a method inside main method. ~ Lesson learned

2 - One can't define a method inside another method. ~ Lesson learned


Here's a suggestion: don't make things more complicated than they have to be. Language designers would never describe your problem that way. Here's how they would write it:

1 - One can't define a method inside another method.


The "main" method is a method, just like any other method, as far as this rule is concerned. There's no need to mention it separately in its own rule. I know when you're trying to understand a concept you get different pieces of it at different times, and that's what you did there. You just need to go through the final step to simplify your understanding as far as possible.

This doesn't just apply when you're understanding a concept. It also applies when you're designing and writing code, and it's the same process. Often when you start designing something, you think you have to have A, and B, and C. But then you think about those things a bit and you realize they are all the same thing, you just came upon them in different contexts, and you really just have to have X. If you didn't go through that final step, you would end up with a lot of duplicated code, whereas a more experienced programmer would take your code and simplify it by removing a bunch of junk.

It's partly true that as you get more practice with reading and writing code, you'll be able to go through the simplifying process more easily. It's also true that some people are better at it than others are.
Kareem Adams
Greenhorn

Joined: Sep 22, 2007
Posts: 6
Thanks a lot Paul.

I appreciate that.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another Simple Java Question
 
Similar Threads
Getting Session on web service client
Tidying up my code?
Trying to get manageable arrays to work with
Just a Simple Java Question
trouble inhasNext