Classes & Objects-1
Classes & Objects-1
OUTLINE
Class
Constructor
Method
Object
This keyword
Instance and static
members
OVERCOMING
NERVOUSNESS
Confidence-building strategies
CLASS 5
//illegal constructor
Void Student () {} // it is method not constructor
StudentReg (int id) {…} // neither method nor constructor
Student (int id, string name); // look like an abstract
method
Static Student (int id) {…}
Final Student (int id, string name) {…} //can’t be
static, final, & abstract
abstract Student (string name) {…}
}
11
METHOD
METHOD
METHOD
–Passing parameter
–Java passes arguments by value
OBJECT
Is an instance of a class/type
Example :-
CONT.…
Declaring an object
Declaring a variable to hold an object is just like
declaring a variable to hold a value of primitive
type. Car c;
Instantiating an object
The new operator instantiate a class by
allocating memory for a new object of that type
OBJECT AS UML
Class name
e.g. Student
List of fields
e.g. name, age, sex,…
List of methods
getName()
setName(“Abel”)
19
THIS KEYWORD
INSTANCE MEMBERS
String carName;
without static keyword
public void setName(String
• Hold Different value
name){
for different objects
carName = name;
}
23
STATIC MEMBERS
keyword }
24
STATIC MEMBERS
Static member can be accessed before any objects of its class are
created, and without reference to any object.
The most common example of static member is Main(). main() is
declared as static because it must be called before any objects exist.
Static variables are, essentially global variables.
Restrictions to static