wood burning stoves*
The moose likes Beginning Java and the fly likes classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classes" Watch "classes" New topic
Author

classes

vijay kaushik
Greenhorn

Joined: Jul 11, 2005
Posts: 3
can we intialise variables in a class without a constructor if so how ?
plz give desired examples.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

This is unrelated to Threads, so I'm moving it to Java in General(Beginner).


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Try some of these ...

A static block can initialize static variables.

The variable declaration can include initial values.

The variable declaration can call a method. I just tried this for the first time.

String var = getVar();

A method like getVar must be very careful not to assume the object instance is fully initialized, eg passing a reference to "this" to some other object might get you in trouble.
[ July 11, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: classes