How to modify the objects using java classes

By: Issac Emailed: 1646 times Printed: 2113 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();
    m.add(9).multiply(2).add(11).multiply(2);
    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 = " + i2.read());
    System.out.println("r1 = " + r1.read());
    System.out.println("r2 = " + r2.read());

  }
}

Output:

i2=7
r1=86
r2=86


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

Comments(0)


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 VB.net
Chat Server in VB.net
Chat client in VB.net
How to open and read an XML file in VB.net
How to create an XML file in VB.net
XML and VB.net
How to create an XML file in VB.NET using Dataset
Multi Threaded Client Socket Programming in VB.net
Multi Threaded Server Socket Programming in VB.net
Multi threaded Socket Programming in VB.net
Client Socket Program sample in VB.net

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
wait(), notify() and notifyAll() in Java - A tutorial
XML and Java - Parsing XML using Java Tutorial
Recursion in java
Method Overloading (function overloading) in Java
indexOf( ) and lastIndexOf( ) in Java
Count number of vowels, consonants and digits in a String in Java
Increment and Decrement Operator
isAlive( ) and join( ) in Java
TCP Server and TCP Client in Java
Using One-Dimensional Arrays in Java
Read from a COM port using Java program
Student marks calculation program in Java
Integer: byte, short, int, and long data types in Java
Most Emailed Articles (in Java)
What is Multithreaded programming?
for loop in java
Read from a COM port using Java program
Calculate gross salary in Java
TCP Server and TCP Client in Java
setPriority() and getPriority() in Java
constants and variables
wait(), notify() and notifyAll() in Java - A tutorial
? - ternary (three-way) operator - in Java
Window Object
Why java is important to the Internet
Handling Fractions in Java
Calculate average sale of the week in Java
HTTPS Java - URL Connection program. Using HttpsURLConnection
Type Casting in Java