Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Simple Java Question

 
Kareem Adams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10490
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scratch that. Winston and Joanne already said it.
 
Kareem Adams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21291
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Paul.

I appreciate that.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic