File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classes" Watch "classes" New topic


vijay kaushik

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

Joined: Jul 08, 2003
Posts: 24199

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
I agree. Here's the link:
subject: classes
It's not a secret anymore!