Basics of Defining and Using Classes PowerPoint PPT Presentation

presentation player overlay
1 / 19
About This Presentation
Transcript and Presenter's Notes

Title: Basics of Defining and Using Classes


1
Basics of Defining and Using Classes
  • Chapter 5, Review

2
5.2.1 Designing Classes
  • Default values for the primitive and reference
    data types
  • Long 0
  • Object reference null
  • Char \u0000
  • Float 0.0

3
5.3.2 Variables
  • There is the storage of local or method data, the
    storage of object data and the storage of class
    data.
  • All objects are created in RAM. The area of
    storage that is used is referred to as the heap
    storage.
  • All method data are also created in RAM. That
    area is called stack storage.
  • All class data are also created in RAM. The area
    of storage that is used is referred to as the
    static storage.

4
5.3.3 Instance Variables
  • When one object stores the reference to another
    object, it is described as having a reference.
    This relationship between the two objects is also
    referred to as a "has a" relationship. This means
    that an association between the two objects
    exists through the reference data.

5
5.3.4 Class data and Static Variables
  • Variables that are associated with a class and
    are shared by all objects of the class are known
    as class or static variables.
  • Class variables are created and initialized when
    that class is loaded by the JVM.
  • If the value of the class variable is changed,
    then the new value is available to all objects.

6
5.4.1 The concept of encapsulation
  • Data is kept private.

7
5.4.2 Access modifiers
  • Data that is final yet accessible outside the
    object should be declared public
  • The best way to declare a Class available to
    everyone is
  • public class Porcupine ()

8
5.6.1Method Syntax
  • The method can be understood in terms of three
    sections or parts  
  • The method identifier elements, which include
    modifiers, qualifiers, return-type, and name
  • The method arguments, which include data-types
    received by the method defined inside the
    parentheses ( )
  • The method body defined inside opening and
    closing braces

9
5.6.1Method Syntax
  • The method syntax includes several required
    elements. These include
  • the return-type
  • the method-name.

10
5.6.4 Return value
  • In all instances, the data type of the values
    must be assignment compatible to the data-type
    declared as the return type in the method
    definition.

11
5.6.6 Concept of pass-by-value
  • A reference passed in as an argument cannot be
    changed to refer to another object.

12
5.6.7 Main Method
  • Syntax requirements
  • Access modifier public
  • Variable to store argument data named args
  • The qualifier static
  • Return type void

13
5.6.8 Instance methods
  • Methods that can only be accessed through an
    object of a class are known as instance methods.
  • If the qualifier static is not declared in the
    method definition, the method is considered an
    instance method.
  • Instance methods can also access non-private
    methods and variables of other classes.

14
5.6.9 Class Methods
  • Class methods require that no objects of the
    class be created in order to use these methods.
  • Instance variables and non-static methods cannot
    be directly accessed by static methods of the
    same class.
  • The main method is a static method.
  • Class methods are also known as static methods.

15
5.9.1 Overloading methods
  • Methods are typically overloaded because the user
    wants to have different versions of a method
    accept different input, but have the same name
    since they have similar functions.
  • Example
  • Int DoNothing( double LazyA)
  • Int DoNothing( float LazyA)

16
5.9.1 Initializing data
  • A static block is used to initialize static
    variables of a class. This block is executed only
    once when the class is loaded at run time. The
    static block is defined using the keyword static.
    This block can only initialize static variables
    of the class.

17
5.9.6 Destruction of an object
  • The value of a reference variable can be reset to
    null. The variable no longer refers to an object.
    The object it originally referenced will be
    destroyed if no more references to it exist.

18
5.10.3 Lifetime of an object
  • The lifetime of an object or method variable in
    Java is the duration of the defining code block.

19
5.10.5 Destruction of an Object
  • The value of a reference variable can be reset to
    null.
  • The variable no longer refers to an object.
  • The object it originally referenced to will be
    destroyed if no more references to it exist. This
    typically occurs by the following line of code.
Write a Comment
User Comments (0)
About PowerShow.com