- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Object as Parameter
Till yet, we have only been using the simple types as parameters to methods. However, it is both correct and common to pass objects to methods. For instance, look at the following short Java program :
Java Use Object as Parameter Example
Here is an example program, demonstrates, how to use object as method parameter in Java:
/* Java Program Example - Java Use Objects as Parameters * In Java, Objects may be passed to methods */ class Test { int a, b; Test(int i, int j) { a = i; b = j; } /* return true if o is equal to invoking object */ boolean equalTo(Test o) { if(o.a == a && o.b == b) { return true; } else { return false; } } } public class JavaProgram { public static void main(String args[]) { Test obj1 = new Test(100, 22); Test obj2 = new Test(100, 22); Test obj3 = new Test(-1, -1); System.out.println("obj1 == obj2 : " + obj1.equalTo(obj2)); System.out.println("obj1 == obj3 : " + obj1.equalTo(obj3)); } }
When the above Java program is compile and executed, it will produce the following output:
As you can see, the equalTo() method within Test compares two objects for equality and returns the result i.e., it compares the invoking objects with the one that it is passed. If they holds the same values, then the method returns true, otherwise false.
Notice that the parameter o in method named equalTo(), specifies Test as its type. Although Test is a class type created by the program, it is used in simply the same way as Java's built-in types.
One of the most common uses of the object parameters involves constructors. Often, you will want to construct a new object so that it is initially the same as some existing object. To perform this, you must define a constructor that takes an object of its class as a parameter. For example, here this version of the Box allows one object to initialize another :
/* Java Program Example - Java Use Objects as Parameters * In this program, Box allows one object to * initialize another */ class Box { double width; double height; double depth; /* notice this constructor, it takes an object of the type Box */ Box(Box ob) // pass object to the constructor { width = ob.width; height = ob.height; depth = ob.depth; } /* constructor used when all the dimensions specified */ Box(double wid, double hei, double dep) { width = wid; height = hei; depth = dep; } /* constructor used when no dimensions specified */ Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } /* constructor used when cube is created */ Box(double len) { width = height = depth = len; } /* compute and return the volume */ double volume() { return width * height * depth; } } class Overload { public static void main(String args[]) { /* create boxes using the various constructors */ Box mybox1 = new Box(100, 200, 150); Box mybox2 = new Box(); Box mycube = new Box(7); Box myclone = new Box(mybox1); // create a copy of mybox1 double vol; /* get the volume of the first box */ vol = mybox1.volume(); /* print the volume of the first box */ System.out.println("Volume of mybox1 is " + vol); /* get the volume of the second box */ vol = mybox2.volume(); /* print the volume of the second box */ System.out.println("Volume of mybox2 is " + vol); /* get the volume of the cube */ vol = mycube.volume(); /* print the volume of the cube */ System.out.println("Volume of cube is " + vol); /* get the volume of the clone */ vol = myclone.volume(); /* print the volume of the clone */ System.out.println("Volume of clone is " + vol); } }
When the above Java program is compile and executed, it will produce the following output:
As you will see, when you begin to create your own classes, giving many forms of the constructors is usually required to allow to be constructed in a convenient and efficient manner.
« Previous Tutorial Next Tutorial »