To understand what a class variable is, it may be worthwhile to understand the types and scope of variables.
For instance there is a 'method' variable that is only visible in a method. eg
public exampleMethod()
{
_ int methodVariable = 0;
}
The int methodVariable would be a 'method' variable. This int is only visible (useable) after the {, and it is no longer visible (useable) after the }.
The same concept applies for a class variable. It is visible, or useable in the [scope of the] class.
public class ExampleClass
{
_ private int classVariable = -1;
_ // it is always best to initiate the variable to a default value. for numbers this is usually -1, and objects usually null.
_ private Object exampleObject = null;
}
Some people say they should be initialised in the constructor. They can also have their value changed in the constructor, but it is better to have the default value there originally. [I hope this did not confuse you].
Back to the example, both classVariable and exampleObject are class variables, as they are in scope, or visible, throughtout the whole class. As the other answerer says, it will even be visible, for use, in a method of the class.
As for an example for a random integer, you would need just the int as I have done above [classVariable], but with a different name.
To create a random variable, you could use Math.random(), but as this returns a double, you will need to cast it back to an int. Another problem you may find is, the double returned from Math.random() will be a number between 0 and 1, including 0 and not 1. This number will always be 0 if casting to an int. The way around this is to multiply the random number returned by a specified number. I will provide an example below, as this may be confusing.
If you tried this
private int classVariable = Math.random();
System.out.println("classVariable = ", classVariable);
This would always print out 0.
This is not what you want. What you need is this, so I will place it in a method.
private void exampleGenRandomNum()
{
_ double temp = Math.random() * 100;
_ classVariable = (int) temp;
}
This way, the variable temp will contain a number between 0 and 100, including 0, but not 100.
This is more what you would like.
The reason for having the class variable would / could be for using it between methods. You do not have to pass it around as an argument to the method, and then pass the argument to another method, etc. It is already saved in a 'class' position, and can be used by the whole class.
Another thing to note, if you make the variable private, only the class and the methods in the class will be able to see this variable. If you make it public, everyone with access to the object can see it, and possibly change the value.
2006-10-29 05:19:34
·
answer #1
·
answered by Mark aka jack573 7
·
0⤊
0⤋
In object oriented programming, all you create is a class. A class has variables that can only be seen by the functions in that particular class so they are protected against anyone accidently changing their values.
Class provides accessors and mutators. Access are used to get the value of a class variable and mutators are used to modify the value of the variable. Mutators provide some sort of assertions as well so that a client code does not corrupt the value.
I will only give u a hint:
class MyClass{
private int somevariable;
MyClass(){
//the below will not work properly but just to give you a hint of what it is.
somevariable = Math.random(); //initialization
}
public int getRandom(){
return somevariable;
}
}
2006-10-28 19:54:17
·
answer #2
·
answered by Manish 5
·
0⤊
0⤋