0

I am new to Java and was wondering how to display details through

public static void main(String [] args){

For example I have a classes, and I need make a separate class called ClassSeperate which sole purpose is to create an object with valid values from the NewCar class and print them using

 public static void main(String [] args){

Say i make this class and have the right constructors accessors and such etc etc

Public class NewCar,

private String carMake;
private String carColor
private int carYear
private int kmsDriven;

I want to implement my own values and set the number of kmsDriven to 50.

So I go to the SeperateClass and type in

public static void main(String [] args){`

NewCar car = new NewCar("FERRARI","YELLOW",2020,50)
System.out.println(?)

I am not quite sure how to print those details i just put into the object in void main. I tried System.out.println(car.toString()); but when i click on the void main nothing pops up? Any help is appreciated!

  • Look up encapsulation and accessor methods. – Turing85 Mar 13 '20 at 20:46
  • 2
    Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Tim Hunter Mar 13 '20 at 20:49
  • `System.out.println(car.toString());` is what you want, assuming your `NewCar` overrides `toString`. – f1sh Mar 13 '20 at 20:50
  • You will either need to override the toString() method, or use getters (methods such as getMake(), getColor(), getYear(), and getDriven(). – NomadMaker Mar 13 '20 at 21:39
  • Wouldpublic static void main ever output a System.out.println? – onesnapthanos Mar 13 '20 at 22:02

2 Answers2

2

Here is a little example

public class foo {
  private String firstName;
  private String lastName;

  public String getFirstname () {
    return firstName;
  }

  public String getLastname () {
    return lastName;
  }

  public String toString () {
    return firstName + " " + lastName;
  }
}

Now you can print both names via

foo f = ...;
System.out.println(f.getFirstName() + " " + f.getLastName());

or

for f = ...;
System.out.println(f.toString());

--

FYI- In Eclipse and many IDEs, if you declare your member variable (firstName,lastName) the IDE can generate getters and setters for you

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
1

Put the following function in NewCar class:

@Override
public String toString() {
    return "NewCar [carMake=" + carMake + ", carColor=" + carColor + ", carYear=" + carYear + ", kmsDriven=" + kmsDriven
            + "]";
}

Now, when you print the car by using the statement, System.out.println(car);, you will get the following output:

NewCar [carMake=FERRARI, carColor=YELLOW, carYear=2020, kmsDriven=50]

Note that I haven't written the toString method myself. My IDE generated it for me e.g. I am using Eclipse IDE where I right-click > Source > Generate toString to get it auto-generated.

If you do not override toString function in NewCar class and try to print car by using System.out.println(car);, you will get the output as mentioned here.

I hope, it helps. Feel free to comment in case of any doubt/issue.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • thank you! I am using a basic compiler blueJ, Would public static void main ever output a System.out.println? – onesnapthanos Mar 13 '20 at 21:58
  • You are most welcome. For compilation and result perspective, it doesn't matter whether you are using Eclipse or BlueJ. If you put the `toString` method mentioned in my answer, you will get the same result irrespective of the IDE you use. IDEs are different in terms of utility features they provide e.g. BlueJ may auto-generate the `toString` method in a different way or even it may not have the feature to auto-generate the `toString` method at all. – Arvind Kumar Avinash Mar 13 '20 at 22:23