Gel
Gel

Reputation: 3

Accessing packages in Java

I set the String message in ReadSource class, and I want access it in Driver class from Automotive class methods, but it displays null and not the message. I have tried to print the message in Automotive setName(String) and it displays the correct String, yet I cant acces it in my main class.

My problem can be illustrate by testing my code below.

Can someone help me or explain it ? Thank you very much.

package Driver;

import Util.ReadSource;
import Model.Automotive;
public class Driver {
    public static void main(String args[]){

        Automotive auto = new Automotive();
        ReadSource read =  new ReadSource();

        String main = "Message";
        //set the message and display
        read.set(main);

        //get the message that was set in the read.set(String)
        auto.getMessage();
    }

}
package Util;

import Model.Automotive;
public class ReadSource {


    public void set(String message){
        Automotive auto = new Automotive();
        System.out.println("This messasge is in getMessage in the
  class ReadSource "+ message);
        //Set the name for automotive
        auto.setName(message);
    }
}
//This the the ouput
/*This messasge is in getMessage in the class ReadSource Message
Message is Message
This messasge is in getMessage in the class Automotive null*/

Upvotes: 0

Views: 82

Answers (2)

Abdul Razak
Abdul Razak

Reputation: 259

public class Driver {
public static void main(String args[]){

    Automotive auto = new Automotive();
    ReadSource read =  new ReadSource(auto);

    String main = "Message";
    //set the message and display
    read.set(main);

    //get the message that was set in the read.set(String)
    auto.getMessage();

    System.out.println(auto.getMessage());
}

public class ReadSource {

Automotive auto;

public ReadSource(Automotive auto) {
    this.auto = auto;
}

public void set(String message) {

    System.out.println("This messasge is in getMessage in the  class ReadSource: " + message);
    //Set the name for automotive
    auto.setName(message);
}

}

public class Automotive {

String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getMessage() {
    return name;
}

}

Upvotes: 0

Zachery
Zachery

Reputation: 111

In your ReadSource.set() method. You are creating a new instance of Automotive. Then passing the value of message through the set method again after creating a new instance of Automotive (which is never used again). What you want to do is to make a variable, (technically a field by definition) in the beginning of the class body.

public class ReadSource {
     private String message;

     public void setMessage(String message) {
         this.message = message;
     }

     // make a getter to return it somewhere else
}

Now the message can be accessed in the main class with auto.getMessage(), (or something similar).

Then if in main you were to:

System.out.println(auto.getMessage());

It would print whatever was passed into the setMessage() method.

Upvotes: 1

Related Questions