Well, I did find previous questions about this but for some reason none of them actually explained to me what's wrong with that i'm doing in this small piece of code -
When i'm trying to compile it with netbeans i get these errors and i just can't figure why is that!
C:\Users\user\Documents\NetBeansProjects\JavaApplication3\src\javaapplication3\JavaApplication3.java:16: error: Illegal static declaration in inner class JavaApplication3.robot
static int ccount = 0;
modifier 'static' is only allowed in constant variable declarations
C:\Users\user\Documents\NetBeansProjects\JavaApplication3\src\javaapplication3\JavaApplication3.java:30: error: non-static variable this cannot be referenced from a static context
robot firstrobot = new robot(34,51,"yossi");
First of all you should name your classes with capital first letter (Robot for example instead of robot). Second, you can not havestatic fields inside inner classes. Does Robot class needs to be inner class? Try not making it inner class.
TIP: use CamelCase for naming classes, methods and variables (example: firstRobot instead of firstrobot).
But I read that there must be a main class that should be named as the file itself, hence i added an external class that "keeps" the robot class and the main class in it.. and now when i try to compile it, i still got errors..
error: class, interface, or enum expected System.out.println("numebr of robots is now " + Robot.ccount);
There is a difference between a class and a method.
In you original post, you have:
Since your JavaApplication3 class is public, it must be in a file named "JavaApplication3 .java"
Now...INSIDE your JavaApplication3 class (which means everything from line 04 to 27), you declare an addition robot class:
Your robot class is defined INSIDE of your JavaApplication3 class - that is what is called an inner class, because it is inside another class.
Your main method is also inside your JavaApplication3 class, but outside of the robot class.
You can move the robot class OUTSIDE of the JavaApplication3 class, but still keep it in the same file. However, the main method must be kept inside the class, and more specifically, inside the class you pass to java with the "java <classname>".
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
You should have two files. One file for JavaApplication3 class (JavaApplication3.java) in which you put main method and another file for Robot class (Robot.java). Then you can have static fields inside Robot class.
Example of JavaApplication3 class:
Example of Robot class:
As fred mentioned you can put Robot inside same file, but I think it's better practice to put it in new file so your application is better organized and easier to maintain. Also if you put it inside JavaApplication3.java file it can not be public!
Class robot should be without public and it will work. But it is better to create new file Robot.java and place Robot class in this file for reasons I've mentioned in my previous post. Your compiler also told you that public class should be in file with same name as class name but you ignored it.
Actually I saw your post just after replying to fred rosenberger.
Thanks Danijel! Is there a bit more profound explanation why in my last version class robot should be without 'public' and why inner classes can't have 'static' variables?
So I split this piece of code to 2 files. Do I need to 'import' or link the robot.java into the javaapplicatio1.java ? Or is it enough them to be together in the same folder under the same package 'javaapplication3' ?
Public class needs to be in file with same name as class name. If you have class Robot (which is public class) than it needs to be in file called Robot.java. This link provides more detailed explanation. More about static fields inside inner classes you can read on this link.
styopa barunin wrote:So I split this piece of code to 2 files. Do I need to 'import' or link the robot.java into the javaapplicatio1.java ? Or is it enough them to be together in the same folder under the same package 'javaapplication3' ?
It is enough to put them in same package. You only need to import classes from other packages.
when I'm trying to compile it i get -
error: package Robot does not exist
C:\Users\user\Documents\NetBeansProjects\JavaApplication1\src\javaapplication3\JavaApplication1.java:7: error: cannot find symbol
symbol: class Robot
location: class JavaApplication1
Remove import Robot.java. Or you can use import javaapplication3.Robot (without .java extension, don't use .java extension for import). But again I repeat you do not need to import classes from same package. Learn to use your IDE he helps with this kind of situations.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database