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

Any concept behind this code?

 
joseph
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Hello
{
System.out.println("x"); //Problem in this line
Hello()
{
System.out.println("4456");
}
public void mix()
{
System.out.println("7899");
}
public static void main(String[ ] args)
{
Hello h = new Hello();
//h.mix();

}
}
--------------------------------
o/p:

Hello.java:4: <identifier> expected
System.out.println("x");
^
Hello.java:4: illegal start of type
System.out.println("x");
^
2 errors
------------------------------------------
Why it is not printing "x" in the console?
 
amit gupta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Put this line..

[System.out.println("x");]

within a constructor or within a method..

it wd solve your problem.

would love to know the explaination!

Thanks,
Sumit
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something that should have been asked in the Java Beginners forum - it doesn't have anything to do with JME.

However, the reason it won't compile is simple - the println call isn't in a method, it's just floating loose in the class definition.
 
amit gupta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

can we say "System.out.write(10);" outside a method? I think write is a method that overrides write() in class FilterOutputStream?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't put any executable statements in a class body outside of any method or block. The only things you can have there are member variable declarations like

int size = 0;
Date now = new Date();
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we say "System.out.write(10);" outside a method?

You can use it in a constructor or a static initializer, otherwise, no.

I think write is a method that overrides write() in class FilterOutputStream?[/QB]

It does, but that has nothing to do with where it can be used.
 
amit gupta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf.

The way you explain things really impressing me for some time being.

I am bit afraid of the sentence

the println call isn't in a method, it's just floating loose in the class definition
by Tim.

Could you please explain the meaning of floating loose..

Thanks in advance.
Sumit
 
om joshi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i want to give the SCJP certification pls tell me some easy tips so that i can do it efficiently........
i want to build my basics Pls gv some guideLines....
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
om joshi, welcome to JavaRanch.

Please do not post unrelated questions to existing topics. That's called "hijacking", and is generally frowned upon. Start a new topic instead.

Your question in particular fits better into the Programmer Certification (SCJP) forum.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sumit gupta:

I am bit afraid of the sentence by Tim.
Could you please explain the meaning of floating loose..
Thanks in advance.
Sumit

Here is your code, annotated a little:

I have marked where each method starts and ends. The line in italics is NOT IN ANY METHOD. you cannot have an executable statement that is not contained inside a method - basically only variable delarations/initializations. if i asked you "When exactly would that line run?", what would you tell me? that line is not in any constructor, so it won't run when the object is created. there is no method call that would run it, so it won't no matter what method you call... the compiler thinks you put it in there by accident, or on the wrong line by mistake. it's kind of 'out there' where it doesn't belong.
 
amit gupta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,

The explaination is really good and I have understood the whole thing with logic behind the same.


Thanks for the reponse..
Sumit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic