Download presentation
Presentation is loading. Please wait.
Published byJemimah Greene Modified over 8 years ago
1
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0
2
C ONTENT Fundamentals of Java classes Reference variables Creating objects Fundamentals of methods Class and instance methods Parameters and arguments Using the keyword this Constructors Fields Comparisons among variables categories Loading classes 1
3
C ONSTRUCTORS A constructor of a class is syntactically similar to a method, but with no explicit return type, which initializes an object or performs any other startup procedures required to create a fully formed object when it is created. Each class has at least one constructor. If there is no constructor specified in a class, javac automatically adds a default constructor ( 預設建構子 ). A default constructor is a non-arg, i.e. without parameter, constructor and does nothing, except calls the non-arg constructor of its super class. If there is a constructor defined, no default constructor will be added by javac. Fall. 2014 2 Java Programming
4
C ONSTRUCTORS The name of a class constructor must be the same as the class name. A class may have more than one constructor, but with different parameter list. Constructors are only activated by the new operator. Fall. 2014 3 Java Programming
5
E XAMPLE OF USING CONSTRUCTORS class MyClass { int x, y; MyClass() { x = 0; y = 0; } MyClass(int a) { x = a; y = 0; } MyClass(int a, int b) { x = a; y = b; } } class DemoMyClass { public static void main(String args[]) { MyClass obj1 = new MyClass(); MyClass obj2 = new MyClass(5); MyClass obj3 = new MyClass(4, 3 + 4); DemoMyClass obj4 = new DemoMyClass(); } Fall. 2014 4 Java Programming Remember javac automatically adds a default constructor for DemoMyClass class.
6
this KEYWORD When a method is called, it is automatically passed an implicit argument that is a reference to the object receiving a message (that is, the object on which the method is called). This reference is named this. Fall. 2014 5 Java Programming
7
E XAMPLE OF USING this class Pwr { double base; int exp; double val; Pwr(double base, int exp) { // instance fields base and exp are // hidden by parameters with the same name this.base = base; // this.base refers to the instance field // base refers to the parameter this.exp = exp; this.val = 1; // both this.val and val refer to the instance field if(exp==0) return; for( ; exp>0; exp--) this.val = this.val * base; } double get_pwr() { return this.val; } Fall. 2014 6 Java Programming
8
C OMPARISON VARIABLE CATEGORIES Fall. 2014 7 Java Programming Analysis FactorFieldsParametersLocal variables UsageStoring class or object properties Passing input values to methods Temporarily storing computation results Where declaredInside a class definition, but outside a method definition In the method headerInside a method definition or a code block How declaredtype name [ = value];type name;type name [ = value]; How initializedDefault, explicit, or by a constructor By the caller during invocation Explicit initialization, NO DEFAULT ScopeWithin class or outside the class Within the methodWithin the method or specific statement block
9
L OADING CLASSES When a class is referred, as an argument of the command java or used by executing a class, the class is loaded into JVM memory space by the class loaders. JVM creates a java.lang.Class object to represent the loaded class. The java.lang.Class class provides many services to process classes. The reflection mechanism can be done by using these services provided by Class class. Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. Fall. 2014 8 Java Programming
10
E XAMPLE OF USING java.lang.Class class DemoClass { public static void main(String args[]) { DemoClass obj = new DemoClass(); Class classObj = obj.getClass(); // get the Class object // representing the class from // which obj is created System.out.println(“The class name of obj: “ + obj.getName()); } Fall. 2014 9 Java Programming
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.