Hey I got Stuck on Constructor exercise can you please help me out?


#1

Hey I got Stuck on Constructor exercise can you please help me out?

my code is below:

public class Dog
{
//The class constructor for the Dog class
Dog()
{

}
public void name1()
{
String name="Tommy";
System.out.println(name);
}
public static void main(String[] args)
{
Dog Tommy=new Dog();
Tommy.name1();
}
}


Hey Guys Do You Have This Code I'm Stuck? Lol :D
#2

I will try, at least :slight_smile: But I am not sure if I recognize this exercise. It is this one -> https://www.codecademy.com/en/courses/learn-java/lessons/object-oriented-programming/exercises/classes-constructors?

If yes then you have some extra code that is not really needed. Method name1 and code in the main method.

But the only problem with your code is fact that you have omitted the public keyword before defining the constructor:

public class Dog {
  //The class constructor for the Dog class
  public Dog() { // <-- here
    
  }

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

Let me know if this helps.


#3

hey i am stuck at the same thing too..... i feel like i am right but it keeps tellng me i have errors?


#4

You should create a new discussion. And please post your code and the error message :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.