• Post Reply Bookmark Topic Watch Topic
  • New Topic

Netbeans and non-static variables  RSS feed

 
styopa barunin
Greenhorn
Posts: 7
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 -


JavaApplication3.java



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");



Thanks in advance!
 
Danijel Mitar
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all you should name your classes with capital first letter (Robot for example instead of robot). Second, you can not have static 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).
 
styopa barunin
Greenhorn
Posts: 7
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>".
 
Danijel Mitar
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
styopa barunin
Greenhorn
Posts: 7
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So as a conclusion of what you say, it supposed to be structured this way




But now i have another error: error: class Robot is public, should be declared in a file named Robot.java public class Robot {
 
Danijel Mitar
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
styopa barunin
Greenhorn
Posts: 7
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
styopa barunin
Greenhorn
Posts: 7
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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' ?

JavaApplication1.java



Robot.java
 
Danijel Mitar
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Danijel Mitar
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
styopa barunin
Greenhorn
Posts: 7
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i simply need to 'import Robot.java' within the main file? (which is JavaApplication1.java) ? Or how does it work? Because it's still not working :\ sorry for bothering you so much..
 
Danijel Mitar
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error message do you get?
 
styopa barunin
Greenhorn
Posts: 7
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaApplication1.java



when I'm trying to compile it i get -
error: package Robot does not exist
import Robot.java;
C:\Users\user\Documents\NetBeansProjects\JavaApplication1\src\javaapplication3\JavaApplication1.java:7: error: cannot find symbol
Robot firstRobot;
symbol: class Robot
location: class JavaApplication1
 
Danijel Mitar
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!