New operator and methods.15


Published on

  • Be the first to comment

  • Be the first to like this

New operator and methods.15

  1. 1. new operator and methods http://improvejava. 1
  2. 2. ObjectiveOn completion of this period, you would be able to know • new operator • Methods http://improvejava.b 2
  3. 3. RecapClass : • 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
  4. 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 typesyntax 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
  5. 5. new operator• Creating objects of a class is a two- step processStep 1• Declare a variable of the class type. null• eg. : Student s; sStep 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
  6. 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
  7. 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 youcannot manipulate references as you can do with pointers http://improvejava. 7
  8. 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
  9. 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
  10. 10. Introduction To Methods• Classes usually consisting of two things • Instance variables • Methods• Instance variables : these are the variables declared insidea class eg.: class A { int x; // x is an instance variable } http://improvejava. 10
  11. 11. Introduction To Methods• Methods are functions declared in a class• A method will perform a specified task related to theclass 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); } } 11 11 9CM604.15
  12. 12. General Form Of A MethodThe 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
  13. 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 thearguments passed to the method when it is called http://improvejava. 13
  14. 14. General Form Of A Method contd..•If the method has no parameter, then the parameter list will beempty• Method that have a return type other than void return a value to thecalling routine using the following form of the return statement return x; Here, ‘x’ is the value returned http://improvejava. 14
  15. 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
  16. 16. Calling A Methodclass 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
  17. 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
  18. 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 18 9CM604.15
  19. 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
  20. 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 20 9CM604.15 20
  21. 21. SummaryIn this class, you have learnt• ‘new’ operator• Usage of new operator• Methods 21 9CM604.15 21
  22. 22. Quiz1. Objects memory is created a. statically b. dynamically c. none 22 http://improvejava. 22
  23. 23. Quiz contd..2. What is new? a. operator b. method c. variable d. none 23 23 9CM604.15
  24. 24. Quiz contd..3. What is reference? a. variable b. kind of pointer c. object d. none 24 http://improvejava. 24
  25. 25. Quiz contd..4 . What operator is used to access a. @ b. ^ c. * d. .(dot) 25 25 9CM604.15
  26. 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
  27. 27. Assignment1. 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 operations2. 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