Syntax of declaring variable and constructor in java
Variable declaration syntax:
int i;
public int i;
int i=10;
final int i=10;
public static final int i=10;
transient int i=10;
volatile int i=10;
Syntax:
<access_specifier> <access_modifier> data_type var_name/identifier <= initializer/value/literal>;
access_specifier:
all a_s are applicable on variables
- private: accessible only within the class
- default: accessible only within the package
- protected: accessible within the package, only upto sub class outside the package
- public: anywhere (within and outside the package)
access_modifiers:
- abstract: is applicable only on class and methods because only class and method are having body/implementation. variables are just initialized.
- final: var whose value want to be constant forever are declared as final. final var value must be initialized at the time of declaration. They can't be reinitialized. initialization means assigning value.
minAgeForVote
public final int MIN_AGE_FOR_VOTE = 18;
class Emp {
int eid; // aka instance/dynamic variables, memory exist in object memory
String ename; // instance variable
double sal; // instance variable
String desig; // instance variable
static final int MIN_AGE=18;
// memory is allocated in class memory; static variable/class variable
static int counter; // aka class variable; memory exist in class memory
Emp() {
counter=counter+1;
}
}
Emp e1=new Emp(); // 32 bytes
System.out.println(Emp.counter);
Emp e2=new Emp(); // 40 bytes
System.out.println(e2.counter);
.
.
.
Emp e100=new Emp(); 40 bytes
System.out.println(e100.counter);
static:
- While declaring vars, vars which are not prefixed with static keyword are called as non static vars/class instance var/dynamic var.
- To such variables as many objects/instances of a class are created in those many instances the memory will be allocated to variables.
- The var value changes from instance-instance hence they are called as dynamic variables.
- Variable which is prefixed with static is called as static var/class var/not a class instance variable.
Generally final variables are declared as static also because final variables value doesn't change though as many number of instances of the class are created, but in every instance memory, memory will be allocated non-static which unneccessarily consumes the memory. Hence not to consume memory unneccessarily for final variable, the var is also declared as static.
- Every static variable may not final such as counter variable incremented in Student class constructor will tell the count how many number of instances of Student class are created.
- If a variable is declared as static in one super class, with the same variable name if one more variable is declared in sub class. static vars are not derived into sub class, they will be directly called using classname.varname. s
- tatic variables should not be called with this and super keywords. But they can be called with super class or sub class instances.
transient: variable which cannot be serialized
Vars declared as transient cannot be persistent. Persistent means in a Emp or Student class we will declare vars such sid, sname, email, mobile, course, create object of Student class, initialize its state and can be stored into .ser file or the object can be passed into n/w to other machine. In the process of object serialization except transient variables all other variables are serialized.
volatile:
variable which cannot be synchronized
- When a single class instance is used in multiple threads, the object state can be accessed or modified in multiple threads. Hence they are thread-safe. To keep object state in thread-safe we can synchronize threads. During thread synchronization except volatile variables all other vars are in synchronous state. Volatile variables are asynchronous.
- native and synchronized cannot be applied on variables.
Constructor syntax:
class A {
}
javac A.java
A.class
class A {
A() {}
}
package pack1;
public class A {
A() {
}
protected A(int a, int b) {
}
}
class A {
int a, b;
A() {}
A(int x, int y) {
a=x;
b=y;
}
}
class B extends A {
int c, d;
B() {
super();
}
B(int x, int y) {
super(x, y);
}
B(int x, int y, int z, int u) {
super(x, y);
c=z;
d=u;
}
}
A a=new A(1,2);
B b=new B(1,2);
B b=new B(1,2,3,4);
Signature of the method/constructor means:
No of parameters
Order of parameters
Types of parameters
Syntax:
<access_specifier> Constructor(<param_type param_name, ....>) <throws Exception1, Exception2> {
}
access_specifiers:
private: If constructor is declared as private such classes cannot be derived or instantiated. We can't subclass, we can't create object.
class A {
static A a;
private A() {
}
// following factory methods returns A class object
static A getInstance() {
if(a==null) {
a=new A();
}
return a;
}
void xyz() {}
void lmn() {}
static void pqr() {}
static void mno() {}
}
A class cannot be instantiated:
A a=new A(); // error
A class cannot be sub classed:
class B extends A {
B() {
super();
}
}
A singleton class object can be obtained:
A a=A.getInstance();
a.xyz();
a.lmn();
A.pqr();
A.mno();
Usually singleton classes (class which contains only one instance per JVM) constructors are declared as private.
Without instantiating and sub classing how a class can be useful.
Hence in such classes, the class itself contains its own instance. The instance is created and returned from one more static method of the same class called factory method. Such classes are called as Singleton classes.
Some of the Singleton classes in java are:
Resource Bundle, Runtime, Logger, Toolkit, DocumentBuilderFactory, Document Builder, System and Math.
default:
class which contains default access specifier constructor can be sub classes and instantiated only within the package. It cannot have sub classes or instantiated classes outside the package.
public class A {
A() {
}
}
Note: In a class if we don't write constructor by default javac provides one default constructor. The default constructor may be public or default access specified with no parameters.
protected:
If class constructor is declared as protected then the class can be sub classes and instantiated both within the package. But outside the package the class can only be sub classes, cannot be instantiated.
public class A {
protected A() {
}
}
public:
- constructor containing public access specifier can have sub class or instantiated class within and outside the package.
access modifiers:
Constructor don't accept any access modifiers.
Example on constructors:
public class Emp {
int eid;
String ename;
double sal;
String desig;
double grossSalary;
double netSalary;
// public no param constructor
public Emp() {
super(); // implicit statement
}
// single parameter constructor
// with pk property as a parameter
public Emp(int eid) {
super(); // implicit statement
this.eid=eid;
}
// two parameters constructor
// with required/essential variables as a parameters
public Emp(String ename, String desig) {
super(); // implicit statement
this.ename=ename;
this.desig=desig;
}
// public paramater constructor
// with all class instance variables passed as parameters
public Emp(int eid, String en, double sal, String desig) {
super(); // implicit statement
this.eid=eid;
ename=en;
this.sal=sal;
this.desig=desig;
}
}
class EmpClient {
public static void main(String rags[]) {
Emp e1=new Emp();
Emp e2=new Emp(1);
Emp e3=new Emp("ABC", "Soft Engg");
Emp e4=new Emp(1, "ABC", 50000.00, "Soft Engg");
}
}
0 Comments