File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 445

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

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 8 API documentation
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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: 46340
You would also find your code easier to understand yourself if you indented it.
I agree. Here's the link:
subject: Object Orientation[Compile Error]
It's not a secret anymore!