• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another Simple Java Question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 62
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scratch that. Winston and Joanne already said it.
 
Kareem Adams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Paul.

I appreciate that.

 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic