Two Laptop Bag*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object Orientation[Compile Error]" Watch "Object Orientation[Compile Error]" New topic

Object Orientation[Compile Error]

Isuru Buddhika

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

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: 14074

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

Joined: Oct 02, 2003
Posts: 11172

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

Joined: Oct 13, 2005
Posts: 38087
You would also find your code easier to understand yourself if you indented it.
Don't get me started about those stupid light bulbs.
subject: Object Orientation[Compile Error]
Similar Threads
difference between static local variables and static global variables.
final variables
Why it want super class constructor
Inner Class