1

Sorry, I am very new to programming for university. This is a practise question for our test and is set up on code runner. The entire main method and all the classes, methods, constructors and variables were already give to me and I have to make the shown class to print out what I have written. But Bride.getAge() and Location.getSuburb() in the println will not work. Do I need to add something else in?

public class Location {

    public static void main(String[] args) {

        Bride person = new Bride("Amy Cronos", 29);
        Location place = new Location("Tonsley", "South Rd");
        Wedding wed = new Wedding(person,place);

        show(wed);
    }

    public static void show(Wedding wed){  
        System.out.println("Wedding data:" );
        System.out.println("Bride: " + wed.getBride() + ", age: " + Bride.getAge);
        System.out.println("Location: " + wed.getPlace() + ", suburb: " + Location.getSuburb());
    }

    private String suburb;
    private String street;

    Location(String suburb, String street){
        this.suburb = suburb;
        this.street = street;
    }

    public String getSuburb(){
          return suburb;
    }

    public String getStreet(){
          return street;
    }

    public class Bride {
        private String name;
        private int age;

        Bride(String name, int age){
            this.name = name;
            this.age = age;
        }

        public String getName(){
            return name;
        }

        public int getAge(){
            return age;
        }
    }

    public class Wedding {
        private Bride person;
        private Location place;

        Wedding(Bride person, Location place){
            this.person = person;
            this.place = place;
        }

        public Bride getPerson(){
            return person;
        }

        public Location getPlace(){
            return place;
        }
    }
}
Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
B.Fall
  • 69
  • 5
  • More precisely​, `wed.getPlace().getSuburb()`. – shmosel Apr 26 '17 at 06:32
  • Possible duplicate [Non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – MadProgrammer Apr 26 '17 at 06:51
  • In the future, please cut down your source code to the minimum that exhibits the behaviour you are curious about. – Michael Piefel Apr 26 '17 at 07:43

2 Answers2

0

You need to correct the below lines in show method

System.out.println("Bride: " + wed.getBride() + ", age: " + wed.getPerson().getAge());

System.out.println("Location: " + wed.getPlace() + ", suburb: " + wed.getPlace().getSuburb());

Since you are using Class names with methods, Bride.getAge() and Location.getSuburb(), these methods are considered static. Hence the error. Since these are not static methods, you need to use the objects of the class to access them rather than Class names.

Pooja Arora
  • 574
  • 7
  • 19
0

you have passed object of class Bride and Location so you need to get value like below code : Try with below code and try to understands object in java , static is different thing .

public static void show(Wedding wed){

System.out.println("Wedding data:" );
System.out.println("Bride: " + wed.getPerson() + ", age: " + wed.getPerson().getAge());
System.out.println("Location: " + wed.getPlace() + ", suburb: " + wed.getPlace().getSuburb());

}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • `wed.getPerson().getAge` is incomplete, you forgot (like OP) to add the `()` – AxelH Apr 26 '17 at 06:44
  • I'm not sure that this will print _exactly_ as OP requires. I think there'll need to be a `toString` method in `Bride` and `Location`. – Dawood ibn Kareem Apr 26 '17 at 06:48
  • `wed.getBride()` `wed.getBride().getAge());` `wed.getPlace()` `wed.getPlace().getSuburb());` when using these methods it prints out like this on code runner: `Wedding data: Bride: Bride@1af1bd6, age: 29 Location: Location@f4c7f77, suburb: Tonsley` maybe the code runner has a bug. – B.Fall Apr 26 '17 at 06:54
  • that's right values you have passes while making objet. are print . – Chetan Joshi Apr 26 '17 at 06:57
  • @B.Fall You can either write `toString` methods in `Location` and `Bride`, as I said earlier; or you can change your code to print `wed.getBride().getName()` and `wed.getPlace().getStreet()` instead of `wed.getBride()` and `wed.getPlace()`. – Dawood ibn Kareem Apr 26 '17 at 07:01
  • @Danwood Ibn Kareem Thank you I just saw your comment, all fixed I was just reading the code wrong as I didn't write it all myself it was given to me. Thank you a lot! – B.Fall Apr 26 '17 at 07:12