-1

I keep getting cannot find symbol error when trying to create the subclass h object in my main code. Would someone please be able to help ? Thanks.

It seems like the main program is accepting the inhtt object but when I try to call h object it says that it cannot find symbol and asks me to create the h object.

public class inhtt {
   //class methods
  public  int thing; 
    public int stuff ;
    public int otherstuff;

  // constructor based on parameters
 public inhtt( int x, int y, int z){
        thing = x;
        stuff = y;
        otherstuff = z;
    }    
 void showmain (){
    System.out.println("thing is " + thing);
    System.out.println("stuff is " + stuff);
    System.out.println("otherstuff is " + otherstuff);
}

public class h extends inhtt {
  int beard; 
  h( int x, int y, int z, int a){
      super(x,y,z);
        beard = a;
  }
  void shownewa(){
    System.out.println("beard is" +beard);  
 }
}
}



 * @author New User
 */
public class runraffharsh {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     inhtt base = new inhtt(1,1,1);
     base.showmain();

     h = new h(1,1,1,1);
     h.shownew();

//      raff beard = new raff(1,1,1,1);
//     beard.showbeard();
//     

    }

}
user3553031
  • 5,990
  • 1
  • 20
  • 40
  • Can you post the errors that you are getting? – user3553031 Jan 06 '15 at 03:14
  • You are calling `h.shownew()` whereas the class `h` has a method called `shownewa()` - typo maybe? – Chris Latta Jan 06 '15 at 03:16
  • Possible duplicate of: http://stackoverflow.com/questions/4951867/cannot-find-symbol-error – Daniel Jan 06 '15 at 03:17
  • yeah a typo, had to change the names of things before post... it doesn't like the h = new h(1,1,1,1); line – user3528592 Jan 06 '15 at 03:17
  • Uncompilable source code - cannot find symbol symbol: variable h – user3528592 Jan 06 '15 at 03:18
  • Your `h` class is defined inside your `inhtt` class. You would reference it as `inhtt.h`. You also don't declare an `h` variable anywhere and can't assign to a class' name like it's a variable. – jpmc26 Jan 06 '15 at 03:21
  • 3
    You have at least three problems. 1) `h` is an inner class of `inhtt`. 2) You're assigning a value to `h` but you've never declared a variable called `h`. 3) You're trying to call `shownew()` when that method doesn't exist. All of these would be a lot easier to see if you used conventional, meaningful names and sensible indentation. – Jon Skeet Jan 06 '15 at 03:24
  • when i move the h class to a new file it works fine, would you be able to explain how i can initiate nested class as i don't understand it from the comments. – user3528592 Jan 06 '15 at 06:17

2 Answers2

2

There are multiple problems with this code:

  1. h is a inner class to inhtt. Since it is not static, you'll need to use something like base.new h(1,1,1,1); to instantiate it.

  2. You need to declare a variable to assign your new h instance to. Try something like inhtt.h h = base.new h(1,1,1,1); for the whole line.

  3. h (the class) has no method named shownew. It has a method named shownewa.

  4. runraffharsh and inhtt are both public classes. They need to be in separate files.

  5. The comment block at the top of runraffharsh is not opened properly.

user3553031
  • 5,990
  • 1
  • 20
  • 40
0

You have problem with inherited class h's reference. And you defined showewa() but tried to access shownew()

Dipto_Das
  • 53
  • 11