Instantiating a Class Accessing Object Members Packages The Access Modifiers
1.3.5 Instantiating a Class
To instantiate a class, we simply use the new keyword followed by a call to a constructor. Lets go directly to an example. class ConstructObj { int data; ConstructObj { initialize data } public static void mainString args[] { ConstructObj obj = new ConstructObj; instantiation } }1.3.6 Accessing Object Members
To access members of an object, we use the dot notation. It is used as follows: object.member The next example is based on the previous one with additional statements for accessing members and an additional method. class ConstructObj { int data; ConstructObj { initialize data } void setDataint data { this.data = data; } public static void mainString args[] { ConstructObj obj = new ConstructObj; instantiation obj.setData10; access setData System.out.printlnobj.data; access data } } The expected output of the given code is 10.1.3.7 Packages
To indicate that the source file belongs to a particular package, we use the following syntax: packageDeclaration ::= package packageName; To import other packages, we use the following syntax: importDeclaration ::= import packageName.elementAccessed; Introduction to Programming II Page 14 J.E.D.I. With this, your source code should have the following format: [packageDeclaration] importDeclaration classDeclaration + Coding Guidelines: + indicates that there may be 1 or more occurrences of the line where it was applied to. Here is an example. package registration.reports; import registration.processing.; imports all classes in registration.processing package import java.util.List; import java.lang.; imported by default imports all classes in java.lang class MyClass { details of MyClass }1.3.8 The Access Modifiers
The following table summarizes the access modifiers in Java. private defaultpackage protected public Same class Yes Yes Yes Yes Same package Yes Yes Yes Different package subclass Yes Yes Different package non-subclass Yes Table 1: Access Modifiers1.3.9 Encapsulation
Parts
» | Komputasi | Suatu Permulaan
» Object-Oriented Design Class Object Attribute Method
» Constructor Package Encapsulation Abstraction Inheritance Polymorphism Interface
» Declaring Java Classes Declaring Attributes Declaring Methods Declaring a Constructor
» Instantiating a Class Accessing Object Members Packages The Access Modifiers
» Encapsulation Inheritance Overriding Methods
» Abstract Classes and Methods Interface
» The this Keyword The super Keyword
» The static Keyword The final Keyword
» Introduction The Error and Exception Classes An Example
» The try-catch Statements Catching Exceptions
» The finally Keyword Catching Exceptions
» Exception Classes and Hierarchy Checked and Unchecked Exceptions
» What are Assertions? Enabling or Disabling Assertions
» What is Recursion? Recursion Vs. Iteration
» Factorials: An Example Print n in any Base: Another Example
» What is an Abstract Data Type? Stacks Queues
» Sequential and Linked Representation Sequential Representation of an Integer Stack
» Linked Lists Linked Representation of an Integer Stack
» Java Collections Abstract Data Types
» Objectives Objectives Objectives Objectives
» The Math Class | Komputasi | Suatu Permulaan
» String Constructors String Methods
» The StringBuffer Class The String and the StringBuffer Class
» The Wrapper Classes | Komputasi | Suatu Permulaan
» The System Class | Komputasi | Suatu Permulaan
» Objectives Command-Line Arguments and System Properties
» Reading from a File Writing to a File
» Divide-and-Conquer Paradigm Understanding Merge Sort The Algorithm An Example
» Objectives Objectives Abstract Windowing Toolkit AWT vs. Swing
» Fundamental Window Classes AWT GUI Components
» Graphics More AWT Components
» The FlowLayout Manager The BorderLayout Manager
» The GridLayout Manager Panels and Complex Layouts
» Setting Up Top-Level Containers A JFrame Example A JOptionPane Example
» Objectives Event Classes | Komputasi | Suatu Permulaan
» ActionListener Method MouseListener Methods MouseMotionListener Methods WindowListener Methods
» Guidelines for Creating Applications Handling GUI Events Mouse Events Example
» Thread Definition Thread States Priorities
» Constructor Constants Methods The Thread Class
» Extending the Thread Class Implementing the Runnable Interface
» Extending vs. Implementing An Example Using the join Method
» An Unsynchronized Example Locking an Object
» First Synchronized Example Second Synchronized Example
» Producer-Consumer Example Interthread Communication
» The Executor Interface Concurrency Utilities
» The Callable Interface Concurrency Utilities
» The ServerSocket and the Socket Class
» The MulticastSocket and the DatagramPacket Class
» The Applet Life Cycle The paint Method
» Objectives Objectives Applet HTML Tags
» Character and Byte Streams Input and Output Streams Node and Filter Streams
» Reader Methods Node Reader Classes Filter Reader Classes
» Writer Methods Node Writer Classes Filter Writer Classes
» A Basic ReaderWriter Example Modified ReaderWriter Example
» InputStream Methods Node InputStream Classes Filter InputStream Classes
» OutputStream Methods Node OutputStream Classes Filter OutputStream Classes
» A Basic InputStreamOutputStream Example Modified InputStreamOutputStream Example
» The transient Keyword Serialization: Writing an Object Stream
» Objectives Why Generics? | Komputasi | Suatu Permulaan
Show more