Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to print out class parameters properly

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How to print out class parameters properly

    I'm trying to get a cloneable interface to work. Here's what I have so far:

    Driver.java
    package interfaces.business;
     
    import java.util.Scanner;
    import java.time.*;
     
    public class Driver implements Cloneable{
     
    	private int DriverID;
    	private String firstName;
    	private String lastName;
     
    	public Driver(int DriverID, String firstName, String lastName) {
    		this.DriverID = DriverID;
    		this.firstName = firstName;
    		this.lastName = lastName;
    	}
     
    	public void setDriverID(int DriverID) {
    		this.DriverID = DriverID;
    	}
    	public int getDriverID() {
    		return DriverID;
    	}
     
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	public String getFirstName() {
    		return firstName;
    	}
     
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	public String getLastName() {
    		return lastName;
    	}
     
    	//To make the clone() method available to all classes (instead of just having protected access),
    	//you can override the clone() method of the Object class with a clone() method that has public
    	//access:
    	@Override
    	public Object clone() throws CloneNotSupportedException{
    		return super.clone();
    	}	
    }

    CloneInterface.java
    package interfaces.ui;
     
    import interfaces.business.Driver;
     
    public class CloneInterface implements Cloneable{
     
    	public static void main(String[] args) {
    		try {
    			System.out.println("The main method from the CloneInterface class is being used.");
    			System.out.println();
    			Driver z = new Driver(5580,"Sam","Peterson");		
    			z.setDriverID(5580);
    			z.setFirstName("Sam");
    			z.setLastName("Peterson");
     
    			//clone the object
    			Driver z2 = (Driver) z.clone();
     
    			//change a value of the cloned object
    			z2.setDriverID(8888);
     
    			//print the results
    			System.out.println(z);
    			System.out.println(z2);
    		}catch (CloneNotSupportedException ex) {
    			System.out.println(ex);
    		}
    	}
    }

    I was expecting the output to look something like this:

    5580, Sam, Peterson
    8888, Sam, Peterson


    But instead, I got something like this:

    interfaces.business.Driver@15db9742
    interfaces.business.Driver@6d06d69c


    What happened, and how do I fix it?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to print out class parameters properly

    I got something like this:

    interfaces.business.Driver@15db9742
    interfaces.business.Driver@6d06d69c


    What happened, and how do I fix it?
    Those are the Strings returned by the Object class's default toString method. If you want to see something different override the class's toString method and have it return the desired Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following 2 Users Say Thank You to Norm For This Useful Post:

    AudacityPlugin (August 29th, 2019), SamJava_the_Hut (June 13th, 2019)

  4. #3
    Junior Member
    Join Date
    Aug 2019
    Location
    Espaņa - Spain
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to print out class parameters properly

    Thanks for the answer norm.
    I have been experiencing the same issue (i have just started with java) and i was driving crazy

    Thaks a lot!

Similar Threads

  1. Creating an object through a class parameters
    By joshhw in forum Object Oriented Programming
    Replies: 2
    Last Post: March 18th, 2014, 08:55 PM
  2. Cannot print boolean value from class!
    By adnan.alvee in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 15th, 2013, 06:12 AM
  3. Replies: 0
    Last Post: January 10th, 2013, 01:36 PM
  4. How do I print item facilities in another class?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 22nd, 2012, 10:43 AM
  5. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM