Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why is this code not running ?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NB both codes where written on a separate file

code 1

package yemi;

public class Zoo {
public String coolMethod() {
return "Wow baby";
}
}

code2


import yemi.Zoo;
class Moo extends Zoo{

public static void main(String args[]) {

System.out.println("A Zoo says, " + coolMethod());

}
}

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you run them? How are you running them?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you can't run it because you can't access non-static methods from a static context.

coolMethod() is non-static and you are trying to use it from a static method.
 
Adeyemi Adeseye
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i get the error message that the package yemi cannot be found when i try to compile the Moo class

and how do you suggest i run the program ?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which command are you using to compile it? In which directory are you when you try to compile it? Is "yemi" a subdirectory of that directory?
 
Adeyemi Adeseye
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the directory where both files are stored is C:/SCJP/Zoo and C:/SCJP/Moo.

I saved both files with Zoo.java and Moo.java respectively.

I tried compiling both files with the command javac Zoo.java and javac Moo.java respectively, and the Zoo.java compiles while the other one gives the error that the package yemi cannot be found.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to read up on Java packages: http://java.sun.com/docs/books/tutorial/java/package/index.html

In short, the package structure needs to match the directory structure, so both classes need to be in the same directory since they're in the same package, and that directory must be named "yemi".

Put both .java files into C:/SCJP/yemi, and then change into the directory C:/SCJP. Then the compilation should work (javac yemi/Moo.java and javac yemi/Zoo.java). You can also compile both classes at the same time through "javac yemi/*.java".
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
simple! You forgot of create new instance to the method. then create an instance of class Zoo or Moo.
 
Adeyemi Adeseye
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, working fine now. thanks so much.
 
reply
    Bookmark Topic Watch Topic
  • New Topic