Data Types:
Data types represent the different values to be stored in the variable. In java, there are two types of data types:
- Primitive data types
- Non-primitive data types
Primitive Datatypes:
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Below table gives more details of these eight primitive data types.
Non-primitive Datatypes:
Also called as reference variables which are an object of the class. These variables are declared to be of a specific type that cannot be changed. For example, Student, String etc.
String is a reference variable but not primitive. When you are creating a variable using String means you are creating an object of String class.
- Class objects and various type of array variables come under reference datatype.
- Default value of any reference variable is null.
- A reference variable can be used to refer any object of the declared type or any compatible type.
Variables are typically used to store information which your Java program needs to do its job. This can be any kind of information ranging from texts, codes (e.g. country codes, currency codes etc.) to numbers, temporary results of multi step calculations etc.
In Java there are four types of variables:
- Local variables
- Instance variables
- Class/Static variables
public class School{
//class/static variable
static int schoolRegNumber = 12345;
//Instance variable
int studentAge;
public int getStudentAge(){
//It's a local variable
boolean isStudent = true;
return studentAge;
}
}
Local Variables:
A local variable in Java is a variable that’s declared within the body of a method. Then you can use the variable only within that method. Other methods in the class aren’t even aware that the variable exists.
From above example in the method getStudentAge(), we declared a boolean variable isStudent which is a local to that method only. Any thing outside method can't see this variable or access it. Thus it is called local variable
Unlike class and instance variables, a local variable is fussy about where you position the declaration for it: You must place the declaration before the first statement that actually uses the variable.
Instance Variables:
Instance variable is the variable declared inside a class, but outside a method without static keyword: something like studentAge in above example.
Now this Student class can be instantiated in other class to use this variable, something like:
public class MainClass{
public static void main(String[] a){
School school = new School();
school.studentAge = 20;
}
}
Variables defined within a class are called instance variables because each instance of the class (that is, each object of the class) contains its own copy of these variables. Thus, the data for one object is separate and unique from the data for another. An instance variable can be declared public or private or default (no modifier). When we do not want our variable’s value to be changed out-side our class we should declare them private. public variables can be accessed and changed from outside of the class. We will have more information in OOP concept .
Class/Static Variables:
These are also known as static member variables and unlike instance variables there's only one copy of that variable that is shared with all instances of that class. If changes are made to that variable, all other instances will see the effect of the changes.
In above example, schoolRegNumber variable is declared as static as a school always have the same registration number and it does not change. So any instance created for the Student class the school registration number is shared and remains same. If changes are made to that variable, all other instances will see the effect of the changes.
Java Constants:
A Constant is a source code representation of a fixed value. They are represented directly in the code without any computation.
This is achieved by using final keyword. Once declared with final keyword, the value of that variable cannot be changed.
Constants can be assigned to any primitive or non-primitive datatype variables. For example −
final int age = 20; final int regNumber = 12345; final float piValie = 3.14; final String schoolName = "Trinity";//non-primitive
Java Access Modifiers:
There are 4 types of java access modifiers:
- private
- default
- protected
- public
If a method or variable is marked as private (has the private access modifier assigned to it), then only code inside the same class can access the variable, or call the method. Code inside subclasses cannot access the variable or method, nor can code from any external class.
Classes cannot be marked with the private access modifier. Marking a class with the private access modifier would mean that no other class could access it, which means that you could not really use the class at all. Therefore the private access modifier is not allowed for classes.
Here is an example of assigning the private access modifier to a field:
public class Clock { private long time = 0; }
The member variable time has been marked as private. That means, that the member variable time inside the Clock class cannot be accessed from code outside the Clock class.
Private Constructors:
If a constructor in a class is assigned the private Java access modifier, that means that the constructor cannot be called from anywhere outside the class. A private constructor can still get called from other constructors, or from static methods in the same class. Here is a Java class example illustrating that:
This version of the Clock class contains a private constructor and a public constructor. The private constructor is called from the public constructor (the statement this();). The private constructor is also called from the static method newClock().
The above example only serves to show you that a private constructor can be called from public constructors and from static methods inside the same class. Do not perceive the above example as an example of clever design in any way.
Default (package):
The default Java access modifier is declared by not writing any access modifier at all. The default access modifier means that code inside the class itself as well as code inside classes in the same package as this class, can access the class, field, constructor or method which the default access modifier is assigned to. Therefore, the default access modifier is also sometimes referred to as the package access modifier.
Subclasses cannot access methods and member variables (fields) in the superclass, if these methods and fields are marked with the default access modifier, unless the subclass is located in the same package as the superclass.
The time field in the Clock class has no access modifier, which means that it is implicitly assigned the default / package access modifier. Therefore, the ClockReader class can read the time member variable of the Clock object, provided that ClockReader and Clock are located in the same Java package.
Protected:
The protected access modifier provides the same access as the default access modifier, with the addition that subclasses can access protected methods and member variables (fields) of the superclass. This is true even if the subclass is not located in the same package as the superclass.
Here is a protected access modifier example:
In the above example the subclass SmartClock has a method called getTimeInSeconds() which accesses the time variable of the superclass Clock. This is possible even if Clock and SmartClock are not located in the same package, because the time field is marked with the protected Java access modifier.
Public:
The Java access modifier public means that all code can access the class, field, constructor or method, regardless of where the accessing code is located. The accessing code can be in a different class and different package.
Here is a public access modifier example:
The time field in the Clock class is marked with the public Java access modifier. Therefore, the ClockReader class can access the time field in the Clock no matter what package the ClockReader is located in.
Private Constructors:
If a constructor in a class is assigned the private Java access modifier, that means that the constructor cannot be called from anywhere outside the class. A private constructor can still get called from other constructors, or from static methods in the same class. Here is a Java class example illustrating that:
public class Clock { private long time = 0; private Clock(long time) { this.time = time; } public Clock(long time, long timeOffset) { this(time); this.time += timeOffset; } public static Clock newClock() { return new Clock(System.currentTimeMillis()); } }
This version of the Clock class contains a private constructor and a public constructor. The private constructor is called from the public constructor (the statement this();). The private constructor is also called from the static method newClock().
The above example only serves to show you that a private constructor can be called from public constructors and from static methods inside the same class. Do not perceive the above example as an example of clever design in any way.
Default (package):
The default Java access modifier is declared by not writing any access modifier at all. The default access modifier means that code inside the class itself as well as code inside classes in the same package as this class, can access the class, field, constructor or method which the default access modifier is assigned to. Therefore, the default access modifier is also sometimes referred to as the package access modifier.
Subclasses cannot access methods and member variables (fields) in the superclass, if these methods and fields are marked with the default access modifier, unless the subclass is located in the same package as the superclass.
public class Clock { long time = 0; } public class ClockReader { Clock clock = new Clock(); public long readClock { return clock.time; } }
The time field in the Clock class has no access modifier, which means that it is implicitly assigned the default / package access modifier. Therefore, the ClockReader class can read the time member variable of the Clock object, provided that ClockReader and Clock are located in the same Java package.
Protected:
The protected access modifier provides the same access as the default access modifier, with the addition that subclasses can access protected methods and member variables (fields) of the superclass. This is true even if the subclass is not located in the same package as the superclass.
Here is a protected access modifier example:
public class Clock { protected long time = 0; // time in milliseconds } public class SmartClock() extends Clock { public long getTimeInSeconds() { return this.time / 1000; } }
In the above example the subclass SmartClock has a method called getTimeInSeconds() which accesses the time variable of the superclass Clock. This is possible even if Clock and SmartClock are not located in the same package, because the time field is marked with the protected Java access modifier.
Public:
The Java access modifier public means that all code can access the class, field, constructor or method, regardless of where the accessing code is located. The accessing code can be in a different class and different package.
Here is a public access modifier example:
public class Clock { public long time = 0; } public class ClockReader { Clock clock = new Clock(); public long readClock { return clock.time; } }
The time field in the Clock class is marked with the public Java access modifier. Therefore, the ClockReader class can access the time field in the Clock no matter what package the ClockReader is located in.
No comments:
Post a Comment