I just started my java tutorials and as part of my first exercise, i was asked to:
a) Create a new class for a real-world object (i chose mobile phone): mobile
b) For this class, create an interface that defines its behavior, then require your class to implement it.
I used Netbeans for this exercise and this is the code i wrote/extended:
package mobile;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
public class Mobile {
int volume = 0;
int ringtone = 0;
void volumeUp (int increment){
volume = volume + increment;
}
void changeringtone (int newValue){
ringtone = newValue;
}
void volumeDown (int decrement){
volume = volume - decrement;
}
void printStates(){
System.out.println("ringtone:" + ringtone + "volume:" + volume);
}
}
However when i run it, i get this error message:
java.lang.NoClassDefFoundError: mobile/Mobile (wrong name: mobile/mobile)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: mobile.Mobile. Program will exit.
I am kindly requesting if anyone can please, first of all, point out to me the mistakes in my code and secondly, how i can fix these problems inside of Netbeans.
Thank you in advance!
karramelle
In this case, you may not even need the main() method unless you are writing code to test your class. As thinksteep suggested, you can move the main() method inside your Mobile class. Alternatively, you can remove it all together in order to compile your code. However, note that you must have a main() method somewhere in order to run your program.