This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Object Orientation[Compile Error] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object Orientation[Compile Error]" Watch "Object Orientation[Compile Error]" New topic
Author

Object Orientation[Compile Error]

Isuru Buddhika
Greenhorn

Joined: Nov 04, 2012
Posts: 11

What is the reason for the following programme to give a compile error?

Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1



Class properties (member variables) are declared inside the class (outside methods). So when the compiler get to that line it expects an identifier as the compiler error says.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

You cannot put arbitrary statements outside methods.

The only thing you can do at class level (outside of a method) is declare variables and optionally initialize them. You cannot put other statements there, such as an assignment statement, which you have in line 4.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Isuru Buddhika wrote:What is the reason for the following programme to give a compile error?

Just a little tip...It is often a huge help if when posting questions like this, you include the compiler error you are getting.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
You would also find your code easier to understand yourself if you indented it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Orientation[Compile Error]
 
Similar Threads
Inner Class
Why it want super class constructor
final variables
difference between static local variables and static global variables.
Operator