Lecture 4 - Programming Syntax
Lecture 4 - Programming Syntax
CS F213
J. Jennifer Ranjani
email: [email protected]
BITS Pilani
Pilani Campus
Class/Object Example
class Student{
int id;
String name;
}
class TestStudent{
public static void main(String args[]){
//Creating object
Student s1=new Student();
//Initializing object
s1.id=253;
s1.name="Sathish";
//Printing data
System.out.println(s1.id+" "+s1.name);
}
}
method name should start with lowercase letter and be a verb e.g.
actionPerformed(), main(), print(), println() etc.
• Types of variables
• Static or class variables
• Instance Variables
• Local variables
System.out.println(obj1.myClsVar);
System.out.println(obj2.myClsVar); System.out.println(myClsVar);
// instance variable
public String myVar="instance variable";
// Creating object
VariableExample obj = new VariableExample();
System.out.println("Calling Method"); Output:
Calling Method
obj.myMethod(); Inside Method
Instance variable
System.out.println(obj.myVar);
}
} BITS Pilani, Pilani Campus
Queries asked during the
lecture
• Can we access static variables before object creation? If
yes, when will the memory for such variables be
allocated.