aspose file tools*
The moose likes Beginning Java and the fly likes doubt in asignment. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in asignment." Watch "doubt in asignment." New topic
Author

doubt in asignment.

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

When i complie this program it give compile error as IDENIFIER EXPECTED.(i=10)

Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

Hi
this is because you cannot initialize a variable that way
just change your code to




Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Thanks,Is it a rule that you can only declare and intialize variables in the same line inside the class,but whereas when i wrote the same code inside a method it compiles succesfully.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

When you want to initilize the fields then you have to guarantee that the initilization value is available and the initilization happens in the same line as declartion. It's kind of a rule for fields initialization.

Well, this kind of initialization is very simpel and plain so, if you want to embed complex logic you can use static initialization blocks, private static methods, final methods or simple initialization blocks without static.

Hope that helps.
[ April 18, 2008: Message edited by: Anubhav Anand ]
Sandilya Garimella
Greenhorn

Joined: Apr 17, 2008
Posts: 3
hi ...
method is a set of instructions so you can declare a variable and assign value to it later.
but where as class is a combination of object behaviours and prperties.
in general no executable statements are written inside a class definition.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Sandilya Garimella,
Welcome to JavaRanch.

Well, what you say is pretty correct and as per the OOP guidelines.
But, here it's a matter of decalring and initializing a vaiable in different lines, which is not feasible as I mentioned that the declaration and initialization go hand in hand on the same line. It's kind of a rule.
[ April 18, 2008: Message edited by: Anubhav Anand ]
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Thanks to ALL.Now i got it.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Anubhav Anand:
[QB
But, here it's a matter of decalring and initializing a vaiable in different lines, which is not feasible as I mentioned that the declaration and initialization go hand in hand on the same line. It's kind of a rule.[/QB]


No, it's not. There's no rule anything like that in the language spec. What there is, is a rule that says that executable statements -- like the assignment i=10 -- can't be put in a class body. The more general statement by Sandilya Garimella is correct.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in asignment.
 
Similar Threads
Gotcha!!
Array Data Print Out
it draws firstly circle and then after a second the circle disappears
Instance blocks.
milliseconds