Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Something wrong with my code.

+5 votes
asked Sep 12, 2022 by Eidnoxon (5,110 points)

Hi!

Can somebody inspect my code and tell me what's wrong? I just started programming and i'm at constructors. But it says symbol not found.

https://onlinegdb.com/fPbsnZN6I

3 Answers

–1 vote
answered Sep 12, 2022 by Peter Minarik (84,720 points)
edited Sep 12, 2022 by Peter Minarik

The problem is that you do not have the fields name, age, and weight in your Human class. I don't know Java that well to know why your code was compiling in the first place. Probably this was just pointing to the same local variable?

I'd fix your problem like this:

Human.java:

public class Human
{
    private String name;
    private int age;
    private double weight;
  
    Human(String name, int age, double weight)
    {
        this.name = name;
        this.age = age;
        this.weight = weight;
    }
    
    public String getName() { return name; }
    public int getAge() { return age; }
    public double getWeight() { return weight; }
}

Main.java:

public class Main
{
    public static void main(String[] args)
    {
        Human human1 = new Human("Matthew", 21, 140);
        Human human2 = new Human("Lucas", 19, 130);
        
        System.out.println(human1.getName());
        System.out.println(human1.getAge());
        System.out.println(human1.getWeight());
        System.out.println();
        System.out.println(human2.getName());
        System.out.println(human2.getAge());
        System.out.println(human2.getWeight());
    }
}
0 votes
answered Sep 14, 2022 by vishwas patel (140 points)
variable "name" is not declared.
+1 vote
answered Sep 16, 2022 by codenxn (1,350 points)

Human.java:

  1. public class Human {
  2.     String name;
  3.     int age;
  4.     double weight;
  5.     Human(String name, int age, double weight) {
  6.         this.name = name;
  7.         this.age = age;
  8.         this.weight = weight;
  9.     }
  10. }
Main.java:
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Human human1 = new Human("Matthew",19,140); blah blah
  4.     }
  5. }
For short: You forgot to declare the name, age and the weight variable.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...