TM2 Classes Objects
TM2 Classes Objects
Static Block
Methods
definitions
Method definitions
(methods handle the behavior)
Methods may be: instance methods or static methods
class Employee{
class Employee int id;
String name;
Instance variables int salary;
• id
• name void setId(int i) {
•salary id = i;
}
Instance methods void setName(String n) {
name = n;
• setId(…) }
• setName(…) void setSalary(int s) {
• setSalary(…) salary = s;
}
•
void getEmployeeDetails( ) {
getEmployeeDetails() System.out.println (name + “ salary
is “ + salary);
}
}
Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 6
Basic information about a class
public class Account { Instance
double balance; Variable
public void deposit( double amount ){
balance += amount; Parameter
}
or argument
public double withdraw( double amount ){
int minimum_balance=5000;
local
if (balance >= (amount+minimum_balance)){
Variable
balance -= amount;
return amount;
}
else {
System.out.println(“Insufficient Balance”);
return 0.0;
} }
public double getbalance(){
return balance;
} } Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 7
Basic information about a class (Contd.).
else {
System.out.println(“Insufficient Balance”);
return 0.0;
}
}
public double getbalance(){
return balance;
}
}
A class variable can also be declared as static where as a local variable cannot be static.
Returns a reference to it
A constructor can be defined with any access specifier (like private, public)
It contains the address of the object through which the function is invoked
Use of this keyword
this can be used to refer instance variables when there is a clash with local variables or
method arguments
this can be used to call overloaded constructors from another constructor of the same
class
We can access static members directly by prefixing the members with the class name
ClassName.staticVariable
ClassName.staticMethod(...)
Static variables:
class StaticDemo
{
private static int a = 0;
private int b;
public void set ( int i, int j)
{
a = i; b = j;
}
public void show( )
{
System.out.println("This is static a: " + a );
System.out.println( "This is non-static b: " + b );
}
java Sample
class Sample{
int i_val;
public static void main(String[] xyz){
System.out.println("i_val is :"+this.i_val);
}
}
java Sample
class Sample{
int i_val=10;
Sample(int i_val){
this.i_val=i_val;
System.out.println("inside Sample i_val:
"+this.i_val);
}
public static void main(String[] xyz){
Sample o = new Sample();}
} Sensitivity: Internal & Restricted © 2017 Wipro wipro.com confidential 25
Quiz- Solutions
Answer 1:
Error: this keyword cannot be referred inside static methods.
Answer 2:
Error: No default constructor created.
Ex :
static {
System.out.println(“Within static block”);
}
The statements within the static block are executed automatically when the class is loaded
into JVM
JVM combines all the static blocks in a class as single static block and executes them
You can invoke static methods from the static block and they will be executed as and when
the static block gets executed
java Sample
class Sample{
public static void main(String[] xyz){
System.out.println("Inside main method line1");
}
static {
System.out.println("Inside class line1");
}
}