aspose file tools
The moose likes Beginning Java and the fly likes Any concept behind this code? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Any concept behind this code?" Watch "Any concept behind this code?" New topic
Author

Any concept behind this code?

joseph
Greenhorn

Joined: Apr 04, 2008
Posts: 3
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

Joined: Dec 29, 2006
Posts: 23
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


SCJP 1.5, SCWCD 5.0
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14573
    
    7

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
amit gupta
Greenhorn

Joined: Dec 29, 2006
Posts: 23
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

Joined: Jul 08, 2003
Posts: 24081
    
  15

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();


[Jess in Action][AskingGoodQuestions]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35446
    
    9
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.


Android appsImageJ pluginsJava web charts
amit gupta
Greenhorn

Joined: Dec 29, 2006
Posts: 23
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

Joined: Apr 17, 2008
Posts: 1

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....


Om Joshi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35446
    
    9
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

Joined: Oct 02, 2003
Posts: 10043
    
    6

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.


Never ascribe to malice that which can be adequately explained by stupidity.
amit gupta
Greenhorn

Joined: Dec 29, 2006
Posts: 23
Hi Fred,

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


Thanks for the reponse..
Sumit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Any concept behind this code?
 
Similar Threads
public class problem
Programming from the command line?
Running compiled code
Any concept behind this code?
Problem with Hello World