How to modify the objects using java classes

By: Issac Emailed: 1587 times Printed: 2040 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

In this tutorial we are going to see how to modify the objects using java classes. 

class ClassA
  private int data;

  public ClassA(int startingVal)
    data = startingVal;

  public ClassA add(int x)
    data += x;
    return this;

  public ClassA multiply(int x)
    data *= x;
    return this;

  public ClassB makeClassB()
    return new ClassB(data);

public class ClassB

  private int data;

  public ClassB(int startingVal)
    data = startingVal;

  public int read()
    return data;

  public boolean nonzero()
    return data != 0;

  public ClassB add(int x)
    return new ClassB(data + x);

  public ClassB multiply(int x)
    return new ClassB(data * x);

  public ClassA makeClassA()
    return new ClassA(data);

  public static ClassB modify1(ClassB y)
    ClassB val = y.add(9);
    val = val.multiply(2);
    val = val.add(11);
    val = val.multiply(2);
    return val;

  public static ClassB modify2(ClassB y)
    ClassA m = y.makeClassA();
    return m.makeClassB();

  public static void main(String[] args) {
    ClassB i2 = new ClassB(7);
    ClassB r1 = modify1(i2);
    ClassB r2 = modify2(i2);
    System.out.println("i2 = " +;
    System.out.println("r1 = " +;
    System.out.println("r2 = " +;




Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Issac
Save/Write/Read image file from/to a database using Java program
How to modify the objects using java classes
Java program for Cloning
Java program for changeable wrapper class
A tutorial on Chat Server and Chat Client in
Chat Server in
Chat client in
How to open and read an XML file in
How to create an XML file in
XML and
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in
Multi Threaded Server Socket Programming in
Multi threaded Socket Programming in
Client Socket Program sample in

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
Using StringTokenizer in Java
InetAddress Example program in Java
Method Overriding in Java
FileReader and FileWriter example program in Java
Vector example in Java
wait(), notify() and notifyAll() in Java - A tutorial
Abstract classes in Java
How to use Iterator in Java
XML and Java - Parsing XML using Java Tutorial
Recursion in java
How to use ArrayList in Java
Method Overloading (function overloading) in Java
Read from a COM port using Java program
Garbage Collection in Java
Most Emailed Articles (in Java)
Understanding isInfinite( ) and isNaN( ) in Java
indexOf( ) and lastIndexOf( ) in Java
equals( ) Versus == in Java
valueOf() in Java
Simple Port Scanner application using Java
How to use Iterator in Java
How to use equals( ) and equalsIgnoreCase( ) in Java
Sample program to demonstrate the use of ActionListener
Using StringTokenizer in Java
ThreadGroup Sample in Java
Using toLowerCase( ) and toUpperCase( ) in Java
Using substring( ) in Java
How to use regionMatches( ) in Java
String Concatenation using Java
How to use ArrayList in Java