This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

illegal start of type println

 
Daniel Lewis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks!
Still learning OOP. Meanwhile I got the feeling that some points are getting clearer, but sometimes -like in the following program, I don't understand, what the problem is:



If I try to compile this I get:
Computer.java:11: error: illegal start of type System.out.println("Hello");

If I remove the System.out.println then it works. So my question is: Why is a println at this position impossible? Using println in methods is ok, but not when defining classes?

Perhaps you can answer me another question too. When i run this program, there is a new notebook object created (by the main class in an other file). Then the methods sell and buy are used "on top" of this object, telling the user "Now you got ... items". Is it possible to get the Name of the object in this println. For example: If it is a notebook Object: Now you got xxx Notebooks, If it is an PC Object: Now you got xxx PCs..."

Thanks in advance!
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class can have- initialization blocks, variable declarations, methods. But you cannot have other statements within the class. They got to be put inside a block({})
 
Campbell Ritchie
Sheriff
Pie
Posts: 48949
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(...); is a statement.
You are not allowed statements at the “top level” in your class; you ought to put it inside a method or constructor (or an initaliser, but I am not fond of initialiser). Unless you need it for debugging, you ought not to put println statements in constructors.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot have printlns directly in the class body - it should be in a method block or so.

Have a instance variable computerType that will hold whether it will be a PC or a desktop, etc. You can assign the variable in the constructor based on the type of computer the object will work on.
 
Daniel Lewis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can assign the variable in the constructor based on the type of computer the object will work on.


So you mean I have to add a variable in my "class Computer" but how can it by "filled" ?

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the Computer class' constructor if you knew at object creation on what type the object will be operating like below -
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic