main() is a static method and hence it cannot access non-static properties. Hence the System.out.print will fail on compile. To avoid compile error, you have to declare the properties as static and you should initialize them. Remember that the properties or Class need not be instantiated when we run a static method.

------------------ Hari Gangadharan Out of the turbulance a soft voice spoke to me ... Son smile and be happy things could be worse... I smiled and I was happy and things became worse!

<B>Hari Gangadharan</B><BR>Unix is user friendly..<BR>but it chooses to whom it is friendly with!

mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91

posted Mar 09, 2001 13:54:00

0

but how shd i access the variables declared in the code without declaring them static? that is the question

The basic principle is that you cannot use a non-static method or variable until the Class is instantiated. Since the Class will not instantiated when we execute the class using java, main should be declared as static. Now since main is static, main or any static methods can only call static methods and can only access static properties. This is because, the other properties need not be available when we run a static methods. Hope it clears your doubts --

------------------ Hari Gangadharan Out of the turbulance a soft voice spoke to me ... Son smile and be happy things could be worse... I smiled and I was happy and things became worse!