Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.

Similar presentations


Presentation on theme: "Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called."— Presentation transcript:

1 Classes and Methods

2 Classes

3 Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called data members Methods –Operations of a class –They operate on variables of the class

4 Example

5 Fields in Class Definition Class variables Instance variables

6 Fields in Class Definition Class Variable –Only one copy is shared with all the objects of the class –Shared among all objects of a class –Exists even if no object has been created –Also called as static fields –Declared using the keyword static –If value is changed, it is reflected for all objects.

7 Instance variables Variables associated with each object A separate value for each instance of a class For example, name, address etc.

8 Example Class Sphere Definition public class Sphere { // class variable static double PI=3.14; // instance variables double xCenter; double yCenter; double zCenter; double radius; }

9 Example

10 Methods in Class Definition Instance Methods Class Methods

11 Execute class methods even when no objects of a class exist Declared using the keyword static. Static methods Can not refer to an instance variable Example: main method Buildin methods in standard class Math

12 Instance Methods Can execute when objects exist. Example: calculate area()

13 Accessing Variables and Methods Accessing Class Methods –double rootPi = Math.sqrt(Math.PI); –Objects can also access in the same way Instance variables and methods –double ballVolume = ball.volume();

14 Defining Classes class Sphere { static final double PI = 3.14; //class variable with fixed value static int count = 0; // Class variable to count objects // Instance variables double radius; // Radius of a sphere double xCenter; // 3D coordinates double yCenter; // of the center double zCenter; // of a sphere // Plus the rest of the class definition... }

15 Defining Methods Self contained block of codes Reusable Can be executed from anywhere in the program Calling a method

16 Basic Structure of a method return_type methodName( arg1, arg2,..., argn ) { code here } Return Statement return return_value; // To return a value from a method

17 Example

18 Parameters and Arguments Parameter – has a name and a type –Appear in definition of the method –Defines type of value to be passed to method when called Arguments –Actual value that is passed to a method when executed –Must be consistent with the type specified in parameter definition

19 Example public static void main(String[] args){... x = obj.mean( 3.0, 5.0 ); } double mean( double value1, double value2 ){ double result = ( value1 + value2 )/ 2.0; return result; }

20 Example

21 Passed by Value double d = 2.0; changeMe(d); System.out.println(d); public void changeMe(double d) { //this has no effect on d outside of this method! d = 345.0; }

22 Passed by Value

23 Class Method Definition class Sphere { // Class definition as before... // Static method to report the number of objects created static int getCount() { return count; // Return current object count }

24 Example Class class Sphere { static final double PI = 3.14; // Class variable that has a fixed value static int count = 0; // Class variable to count objects // Instance variables double radius; double xCenter; // 3D coordinates double yCenter; // of the center double zCenter; // of a sphere // Static method to report the number of objects created static int getCount(){ return count; // Return current object count } // Instance method to calculate volume double volume() { return 4.0/3.0*PI*radius*radius*radius; } // Plus the rest of the class definition... }

25 class Sphere { static final double PI = 3.14; static int count = 0; // Instance variables double radius = 5.0; double xCenter = 10.0; double yCenter = 10.0; double zCenter = 10.0; // Static method to report the number of objects created static int getCount(){ return count; // Return current object count } // Instance method to calculate volume double volume() { return 4.0/3.0*PI*radius*radius*radius; } // Plus the rest of the class definition... public static void main (String[] args){ int count; double volume; count=Sphere.getCount(); System.out.println(count); Sphere s1=new Sphere(); volume=s1.volume(); System.out.println(volume); } }

26 The Variable this Every instance method has a variable with the name this that refers to the current object for which the method is being called void changeRadius(double radius) { // Change the instance variable to the argument value this.radius = radius; }

27 Initializing data members: The ordinary way class Sphere { static final double PI = 3.14; // Class variable that has a fixed value static int count = 0; // Class variable to count objects // Instance variables double radius = 5.0; // Radius of a sphere double xCenter = 10.0; // 3D coordinates double yCenter = 10.0; // of the center double zCenter = 10.0; // of a sphere // Rest of the class... }

28 Using Initialization Blocks There are two kinds of initialization blocks: –static initialization block –non-static initialization block

29 static initialization block A static initialization block is a block defined using the keyword static and is executed once when the class is loaded. A static initialization block can initialize only static data members of the class. A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example: static { // whatever code is needed for initialization goes here }

30 static initialization block class TryInitialization { static int[] values = new int[10]; static { System.out.println("Running initialization block."); for(int i=0; i<values.length; i++) { values[i] = (int)(100.0*Math.random());} } // List values in the array for an object void listValues() { System.out.println(); // Start a new line for(int value : values) { System.out.print(" " + value); // Display values } System.out.println(); // Start a new line } public static void main(String[] args) { TryInitialization example = new TryInitialization(); System.out.println("\nFirst object:"); example.listValues(); example = new TryInitialization(); System.out.println("\nSecond object:"); example.listValues(); }} Running initialization block. First object: 40 97 88 63 58 48 84 5 32 67 Second object: 40 97 88 63 58 48 84 5 32 67

31 Non-static initialization block –non-static initialization block is executed for each object that is created and thus can initialize instance variables in a class. –This block appears without the static keyword. { // whatever code is needed for initialization goes here } Running initialization block. First object: 66 17 98 59 99 18 40 96 40 21 Running initialization block. Second object: 57 86 79 31 75 99 51 5 31 44

32 LAB PRACTICE Compile & execute “TryInitialization” class with static initialization and non-static initialization block. Examine its output.

33 LAB PRACTICE Make a class Bicycle, identify its data members for example “speed=0”, “gear=1” etc. Add methods like changeGear(int), speedUp(int), applyBrakes(), printStates() etc. Make a main method to create 2 bicycle objects and call functions of the class in the following sequence. 1.Change speed 2.Change gear 3.Print states 4.Apply brakes 5.Print states


Download ppt "Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called."

Similar presentations


Ads by Google