How to modify the objects using java classes

By: Issac Emailed: 1676 times Printed: 2157 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)
Stack example in Java - push(), pop(), empty(), search()
FileReader and FileWriter example program in Java
Method Overriding in Java
Read from a COM port using Java program
The Basic Structure of a Simple Java program
XML and Java - Parsing XML using Java Tutorial
Using StringTokenizer in Java
How to use ArrayList in Java
Recursion in java
Student marks calculation program in Java
Method Overloading (function overloading) in Java
How to use Iterator in Java
Abstract classes in Java
append() in Java
Most Emailed Articles (in Java)
Tomcat and httpd configured in port 8080 and 80
Update contents of a file within a jar file
Vector in Java - Sample Program
Get user input in Java
FilenameFilter - sample program in Java
How to Send SMS using Java Program (full code sample included)
Count number of vowels, consonants and digits in a String in Java
Java program using Method Overriding
Using StringTokenizer in Java
How to use ArrayList in Java
Using substring( ) in Java
Java File
MultiLevel Inheritance sample in Java
Read a file line by line in Java - Sample Program
The clone() Method in Java