TYPES, VALUES AND VARIABLES(10)
TYPES, VALUES AND VARIABLES(10)
Heap pollution: When the possibility exists that a variable of a parameterized type refers to an object that is not of that parameterized type. Heap pollution can only occur if the program performed some operation that would give rise to an unchecked warning at compile time.
KINDS OF VARIABLES:
There are 7 kinds of variables:
1. class variables: in a class declaration a field is declared using the keyword static ; in an interface declaration, the field can be with(out) the keyword static.
Creation of class variables: when the class or interface is prepared and then initialized to a default value.
public class ClassVar {
//primitive and reference class variables prepared
static int number;
public static void main(String[] args) {
//they are now initialised, after initialisation
//you've created a class variable
number = 24;
}
}
2. Instance variables: an instance variable is a field declared in a class declaration without the keyword static. When instance variables occur in a class are created and initialized to a default value on the creation of a new object of the class or of any class that subclasses the class. An instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary object finalization[1] has been completed.
public class InstanceVar {
//primitive and reference class variables prepared
static int number;
//an instanc variable next
boolean monkey_tail; //default is false a la jls.
}
public class Loader {
public static void main(String[] args) {
//create an object of InstanceVar
InstanceVar mine = new InstanceVar();
//you'll realise that even without initialising
//it it is given default false
System.out.println(mine.monkey_tail);
//we can still assign it though
mine.monkey_tail = true;
}
}
3. Array components: these are unnamed variables that are created and initialized to default values whenever a new object that is an array is created. The array component effectively ceases to exist when the array is no longer referenced.
public class ArrayComponents {
//an array of integers as components
int[] five = new int[5];
//this method takes all the default values
//according to jls, 0 and prints them out.
void defaultvalue(){
for (int i=0;i<5;i++ ){
System.out.print("The ");
switch(i){
case(0): System.out.print("first ");break;
case(1): System.out.print("second ");break;
case(2): System.out.print("third ");break;
case(3): System.out.print("fourth ");break;
case(4): System.out.print("fifth ");break;
default: ;break;
}
System.out.println("value is: "+five[i]);
}
}
}
public class Loader {
public static void main(String[] args) {
ArrayComponents details = new ArrayComponents();
details.defaultvalue();
}
}
4. Method parameters: method parameters name argument values passed to a method. For every parameter declaration in a method declaration, a new variable is created each time that method is invoked, which is initialized with the corresponding argument value from the method invocation. The method parameter ceases to exist when the execution of the body of the method is complete.
public class MethodExample {
String id;
int tag;
//this method takes as parameters a String name
//and an integer as number.
void setName(String name, int number){
this.id=name;
this.tag=number;
}
}
public class Loader {
public static void main(String[] args) {
MethodExample taketwo = new MethodExample();
//method invocation. this passes the required
//arguments specified by the method parameter.
taketwo.setName("adewale", 42);
}
}
5. Constructor parameters: constructor parameters name argument values passed to a constructor. Each time a class instance expression or explicit constructor invocation invokes a constructor, a new parameter variable is created and initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist when the execution of the body of the constructor is complete.
public class ConstructorExamples {
String name;
int tag;
//constructor without parameters
ConstructorExamples(){
this.name="nobody";
this.tag=0;
}
//this constructor has parameters
ConstructorExamples(String up, int number){
this.name = up;
this.tag = number;
}
}
public class Loader {
public static void main(String[] args) {
//invoking the constructor without parameters
ConstructorExamples beanpole = new ConstructorExamples();
//hey, what does it contain
System.out.println("Name: "+
beanpole.name+". Number: "+beanpole.tag);
//invoking the constructor with arguments
ConstructorExamples magpie = new
ConstructorExamples("elizabeth", 3);
//you contain something too not so?
System.out.println("Name: "+magpie.name+". Number:
"+magpie.tag);
}
}
Footers:
[1]. Object finalization: freeing up resources tat cannot be freed automatically by an automatic storage manager.
No comments:
Post a Comment