wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes class variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "class variables" Watch "class variables" New topic
Author

class variables

mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91

class Op_initial {
public int z;
public int x=1;
public static void main (String ar[]) {
int y;
System.out.print("Member z, x=1 "+" "+z+x);
}
}

i get a compile error on the s.o.p line....why?
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
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
but how shd i access the variables declared in the code without declaring them static?
that is the question
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Create an instance of class and use the object to access instance variables.!!


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
Mansoor Sb,
Eid Mubarak, for this problem u should use
Op_initial OI = new Op_initial();
and then
(OI.z+OI.x)
I am sure that it will work this way.


Seeking Certification
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
thank u fawad. eid mubarak to u as well..thanks for ur sms..and i hope u got me email...
this just means that the class variables cant be directly accessed ... unless they r static.
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
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!
 
 
subject: class variables
 
Similar Threads
question about variables changed inside methods
please explain the output
explanation needed
Chapter 5 K&B -Doubt
x++ , ++x