This document discusses new operators, methods, and classes in Java. It explains that the new operator dynamically allocates memory for a new object and returns a reference. Methods are functions defined within classes that perform specific tasks related to that class. The document provides examples of declaring classes with methods and using the dot operator to call methods on objects.
2. Objective
On completion of this period, you would be able to know
• new operator
• Methods
http://improvejava.b 2
logspot.in/
3. Recap
Class :
• is blue print or plan of an object
• class consists both methods and instance variables
• syntax of class declaration :
class class_name {
instance variables;
methods;
}
Object :
is an instance of a class
3
http://improvejava. 3
blogspot.in/
4. new Operator
• You know how to declare a primitive variable and assign it a
value
eg. : int x = 10;
• What about non-primitive variables? i.e. what about objects?
• When you create a class, you are creating a new data type
syntax of class declaration : Example :
class class-name { class Student {
instance variables; int sno, m1, m2, m3;
methods; void total_mks() {….}
} }
• You can use class to declare objects of that type
4
http://improvejava. 4
blogspot.in/
5. new operator
• Creating objects of a class is a two-
step process
Step 1
• Declare a variable of the class type.
null
• eg. : Student s;
s
Step 2
• Allot the required memory and sno
s
initialize its data members m1
• You can do this using the ‘new’ m2
operator m3
• eg. s = new Student(); Student object
Fig. 14.1 Creating object
http://improvejava. 5
blogspot.in/
6. Use of new Operator
• The new operator dynamically allocates memory for an object
and returns a reference to it
• This reference is the address in memory of the object allocated
by new. This reference is stored in the variable
• Thus, in Java all objects will be dynamically allocated
http://improvejava. 6
blogspot.in/
7. Another Way Of Creating Object
• In the preceding example,
Student s; // declares a reference to object
s = new Student(); // allocates a Student object
• By combining above two statements ,we can also write
Student s = new Student(); // this is same as above
• Object reference is similar to pointers, but the difference is you
cannot manipulate references as you can do with pointers
http://improvejava. 7
blogspot.in/
8. Closer Look At ‘new’
• ‘new’ operator dynamically allocates memory for an object
• The general form of it is
ClassName objVar = new ClassName();
• here, objVar is a variable of the class type being created
• ClassName is the name of the class that is being instantiated
• ClassName followed by parenthesis ‘( ) ‘ specifies the constructor
of the class
http://improvejava. 8
blogspot.in/
9. Advantages Of ‘new’
• Program can create as many or as few objects as it needs
during the execution of your program
• No memory wastage
• No insufficient memory message (unless total
memory is used )
http://improvejava. 9
blogspot.in/
10. Introduction To Methods
• Classes usually consisting of two things
• Instance variables
• Methods
• Instance variables : these are the variables declared inside
a class
eg.: class A {
int x; // x is an instance variable
}
http://improvejava. 10
blogspot.in/
11. Introduction To Methods
• Methods are functions declared in a class
• A method will perform a specified task related to the
class
eg. : class A {
int x; // x is an instance variable
void display( ) {//display is a method to print value of x
System.out.println(“value of x is”+x);
}
}
http://improvejava.blogspot.in/ 11
11
9CM604.15
12. General Form Of A Method
The general form of a method
type method-name([parameter-list]) {
// body of method
} // end of method
eg. :
int add(int x, int y)) {
int res;
res = x + y;
return(res);
} // end of method
http://improvejava. 12
blogspot.in/
13. General Form Of A Method contd..
• A method should return a value
• The type of value returned is called ‘return type’ of the method
• If the method does not return a value, its return type must be void
• Name of the method must be a legal identifier
• Parameter-list is a sequence of type and identifier pairs separated by
commas
• Parameters are essentially variables that receive the value of the
arguments passed to the method when it is called
http://improvejava. 13
blogspot.in/
14. General Form Of A Method contd..
•If the method has no parameter, then the parameter list will be
empty
• Method that have a return type other than void return a value to the
calling routine using the following form of the return statement
return x;
Here, ‘x’ is the value returned
http://improvejava. 14
blogspot.in/
15. Adding A Method To Class
• Methods to do specified task are defined by the class
eg. :
class Student {
int sno, m1, m2, m3;
void total_mks() {
int total_mks;
total_mks = m1 + m2 + m3;
System.out.println(“total marks of the student”+total-mks);
}// end of method
} // end of class
• total_mks() is a method
• Its task is to find the total of m1,m2 and m3
http://improvejava. 15
blogspot.in/
16. Calling A Method
class UseStudent {
public static void main(String args[]) {
Student s = new Student();
s.m1 = 99; s.m2 = 80; s.m3 = 35;
s.total_mks();
}// end of method
} // end of class
http://improvejava. 16
blogspot.in/
17. Calling A Method contd..
• Look closely at the following line Dot operator is used
to access methods in
s.total_mks(); class
• The above statement invokes the total_mks() method
on ‘s’
i.e it calls total_mks() relative to the ‘s’ object,
using the object name followed by the dot(.) operator
17
http://improvejava.b
logspot.in/
18. Calling A Method contd..
• When we call s.total_mks() JVM transfers control to
the code defined inside the class
• After execution of the above method, control return
back to the calling routine
http://improvejava.blogspot.in/ 18
9CM604.15
19. Calling A Method contd..
• A method is always invoked relative to some object
of its class
• Thus, within a method, there is no need to specify the
object a second time
http://improvejava.b 19
logspot.in/
20. Discussion
• Classify Methods based on return type and parameter
passing
• Methods that have no return type, no parameters
• Methods that have no return type, but have parameters
• Methods that have return type, but no parameters
• Methods that have both return type, parameters
http://improvejava.blogspot.in/ 20
9CM604.15 20
21. Summary
In this class, you have learnt
• ‘new’ operator
• Usage of new operator
• Methods
http://improvejava.blogspot.in/ 21
9CM604.15 21
22. Quiz
1. Objects memory is created
a. statically
b. dynamically
c. none
22
http://improvejava. 22
blogspot.in/
23. Quiz contd..
2. What is new?
a. operator
b. method
c. variable
d. none
http://improvejava.blogspot.in/ 23
23
9CM604.15
24. Quiz contd..
3. What is reference?
a. variable
b. kind of pointer
c. object
d. none
24
http://improvejava. 24
blogspot.in/
25. Quiz contd..
4 . What operator is used to access
a. @
b. ^
c. *
d. .(dot)
http://improvejava.blogspot.in/ 25
25
9CM604.15
26. Frequently Asked Questions
Explain the syntax of a method
What is .(dot) operator? What is the use of it?
Explain the mechanism of invoking a method in
Java?
26
http://improvejava. 26
blogspot.in/
27. Assignment
1. Write a class called Student consisting of
pin_no, sname, fname, city as instance variables,
get_stu_details and disp_stu_details as methods
Create an object for the above class, and access both
methods to do the above operations
2. Write a class called Employee consisting of
eno, ename, desgination, tot_salary as instance
variables, get_emp_details and disp_emp_details as
methods. Create an object for the above class, and
access both methods to do the above operations
http://improvejava. 27
blogspot.in/