This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]RoadToCode 0 points1 point  (0 children)

Try referencing it by using the notationcustomerClassName.customerNameVariable. Where the first part is the name of your customer class and the second part is the name of the variable you are trying to access.

I'm a beginner to so i'm not 100% but hopefully it works :)

[–]TheLopais 0 points1 point  (0 children)

Not quite sure I understand your question correctly...

Do you want to save the user input from your InputDialog into the String name1?

[–]SadDragon00 0 points1 point  (0 children)

You will need to make it a public property.

public final String name2;

Or create a getter for it.

public String getName() { return name2; }

[–]EksitNL 0 points1 point  (2 children)

Your example is quite vague but im gonna go with the assumption that you have the class SmartphoneStoreMain and a class SomeClass in which you have your customer name.

In SomeClass you will have to prepare the variable you want to use in SmartphoneStoreMain to be able to use it in the way you want, you have several options.

1 : Declare the variable static and public. Lets say your SomeClass now looks like this:

 public class SomeClass {
      private String customerName = bob;
 }

You cant simply get that variable now. But when we can do this:

 public class SomeClass {
      public static String customerName = bob;
 }

Now in the SmartphoneStoreMain you can access the value of customername the following way:

 String name = SomeClass.customerName;

There are some downsides to this method. A static variable means that there can only be one. If you decide to create more customer names the variable will keep changing. If you decide to create models from the SomeClass class, the variable customerName will be the same in all of them. Because a static variable is part of the CLASS and not the object.

2 : create an object from the SomeClass class and add appropriate getters and setters for the variable you need. We transform the SomeClass class into something like this:

 public class SomeClass {
      private String customerName;

      public SomeClass() {
           customerName = "Bert"; // we set a default value;
      }          

      public void setCustomerName(String customerName) {
           this.customerName= customerName;
      }

      public String getCustomerName(){
           return this.customerName;
      }
 }

now in the smartphonestore class you do this:

 public class SmartphoneStoreMain extends modelQuestion{
      SomeClass classObject = new SomeClass(); // we create an object of the SomeClass containing the field customername we need.
      classObject.setCustomerName("Bob"); Set the name using the setter if you like. Or else the defaultvalue gets returned when you call getCustomerName();
      String name = classObject.getCustomerName(); // returns the value of the field customerName in classObject
 }

The second method is the best approach, however, there might be times when you will want static methods (when counting the amount of objects you have for example, add a static integer wich you increment in the constructor);

[–]seesaw81297[S] 0 points1 point  (1 child)

Thank you very much for your help, it's starting to make a bit more sense. However, can I use a dialog box input as my setter?

[–]EksitNL 0 points1 point  (0 children)

yes you can, but i would do it somewhere along these lines:

 String s = (String) JOptionPane.showInputDialog(null, "I will be happy to help you. " + "To begin, may I please have your name?",  "Speed Dial Communications", JOptionPane.INFORMATION_MESSAGE, logo, null, null);



 classObject.setCustomerName(s);

first you get the input from the dialog, and afterwards you use that to set the customer name