Why wont this work?
Hello
Please tell me whats wrong in my code.
I want to return the xp and level and print it.
Here is my main class:
publicclass main{
publicstaticvoid main(String args[]){
acc Account;
Account =new acc();
System.out.println("level: " + acc.getlevel());
System.out.println("xp: " + acc.getxp());
}
}
And here is my acc class:
publicclass acc{
privateint level;
privateint xp;
privateint hairstyle;
privateint haircolor;
privateint gender;
privateint shirt;
privateint legs;
privateint nextlevel;
acc(){
level = 1;
xp = 0;
hairstyle=1;
haircolor=1;
gender=1;
shirt=1;
legs=1;
nextlevel=1;
}
publicvoid addxp(int xptoadd){
xp += xptoadd;
}
publicint getxp(){
return xp;
}
publicint getlevel(){
return level;
}
publicvoid setlook(int genderchoose,int shirtchoose,int legschoose,int haircolorchoose,int hairstylechoose){
gender = genderchoose;
shirt = shirtchoose;
legs = legschoose;
haircolor = haircolorchoose;
hairstyle = hairstylechoose;
}
publicint gethairstyle(){
return hairstyle;
}
publicint gethaircolor(){
return haircolor;
}
publicint getgender(){
return gender;
}
publicint getshirt(){
return shirt;
}
publicint getlegs(){
return legs;
}
publicint getnextlevelxp(){
if(level == 1){
nextlevel=200;
}
return nextlevel;
}
}
> Please tell me whats wrong in my code.
Please tell us what's wrong in your code.
~
When i try to convert main.java to class file the compile says:
main.java:6: non-static method getlevel() cannot be referenced from a static context
System.out.println("level: " + acc.getlevel());
^
main.java:7: non-static method getxp() cannot be referenced from a static context
System.out.println("xp: " + acc.getxp());
2 errors
I hope you can help me
You get this error because static members don't require an instance of the object to be accessed; they belong to the class. But a non-static member belongs to an instance (an individual object). There's no way for the static method to know which instance's variable to use or method to call, and thus, the compiler happily tells you that you can't access an instance member from a class (static) method.
Note that you're calling methods using the class name, *not* the reference variable. It may help reduce your confusion by following standard Java conventions of lowercase variable names and uppercase class names; you've got that part backward.
~
make your class static, and the functions within too?
also you might wanna put a modifier on your constructor for the acc class otherwise its package or something like that by default, which can give you issues later on depending on what you want to do.
> make your class static, and the functions within too?
Is that a question, or a statement, or just a questionable statement?
well i know it'll fix the problem for sure, but i dont know if that's what he wants to do, since making the class static wont yeild the same as having it non static...
Is this a new take on variable/class naming?
acc Account;
Account = new acc();
I thought it was an error at first.
> well i know it'll fix the problem for sure
No, declaring the class static won't fix that problem. In this case, doing so will create even more compilation problems.
~
> well i know it'll fix the problem for sure, but i
> dont know if that's what he wants to do, since making
> the class static wont yeild the same as having it non
> static...
How do you make a class "static"?
What is the consequence of making a class static?
> How do you make a class "static"?
>
> What is the consequence of making a class static?
*Extracts answer from hindquarters*
~
> well i know it'll fix the problem for sure, but i
> dont know if that's what he wants to do, since making
> the class static wont yeild the same as having it non
> static...
And what about any other classes which are added? They would all have to be static also? You should create an instance of your class and work with this to avoid the error.
> They would all have to be static also?
They'd all have to be nested, that's for sure, but that's by definition. ;o)
~
> > well i know it'll fix the problem for sure, but i
> > dont know if that's what he wants to do, since
> making
> > the class static wont yeild the same as having it
> non
> > static...
>
> And what about any other classes which are added?
> They would all have to be static also? You should
> create an instance of your class and work with this
> to avoid the error.
and that's why i orignaly posted it as a suggestive question, because it would get rid of that error, and like I said it all depends on what he's trying to do... because making everything static would make the error message go away, but like i mentioned, this might not be what he wants depending on what he wants do to....
> > They would all have to be static also?
>
> They'd all have to be nested, that's for sure, but
> that's by definition. ;o)
>
> ~
Yep, I should have mentioned methods which was what I was referring to. I used to make the same mistakes, starting out in main which is static and fixing the problem by making everything else static to avoid errors.
that might not be what he wants though. He might get some behaviors that he doesn't want, unless his app is pretty much just the code he posted.
> and that's why i orignaly posted it as a suggestive
> question, because it would get rid of that error...
No, it wouldn't. I'll try to be more direct. Simply declaring the class static in this case (which you suggested) would introduce errors, rather than correct any. The acc class is shown as a top-level class. The static modifier is not allowed for top-level classes.
~
> make your class static, and the functions within
> too
>
> also you might wanna put a modifier on your
> constructor for the acc class otherwise its package
> or something like that by default, which can give you
> issues later on depending on what you want to do.
i said, make the functions static... a static class with non static functions would jsut give him the same errors he's getting now, but for everysingle one of his functions.
> > make your class static, and the functions within
> > too
> i said, make the functions static...
Have a closer look at what you actually wrote. Also, in Java, what you are calling "functions" are called "methods".
> a static class with non static functions would jsut give him the
> same errors he's getting now, but for everysingle one
> of his functions.
No, it wouldn't. Declaring the class static would create a different error.
Good gravy. Do you understand what a static class is?
~
yeah sorry, i know what it means, I did not realize i said make the class static...
I meant make the methods static. But what i am saying is, he might not want to do that, because then it'll start giving erros about his variables if he starts doing other things. so I was saying, maybe another way of writting his code might be a good idea, where there wouldn't be that problem.
did not mean to say make the class static, that would be bad, unless he's only running that little chunk of code, lol.
making his method static will mean he has to make is variable static which means that the variable does not behave the same as before, and in the long run, it might be a bad thing.
> I meant make the methods static.
And you know that alone won't work, either, right?
> did not mean to say make the class static, that would be bad
Not necessarily. There may be a decent enough design reason to make "acc" (the class begs for a useful name) a nested class. But such a decision is completely unrelated to the problem at hand; namely, that the OP is trying to call instance methods as if they were class methods.
~
just realized we are really dumb, here is the fix...
public static void main(String [] args)
{
acc Account=new acc();
System.out.println("level: " + Account.getlevel());
System.out.println("xp: " + Account.getxp());
}
change acc.getlevel() and acc.getxp to Account.getlevel and Account.getxp
I feel dumb for not seeing it before, enjoy your error free code.
> just realized we are really dumb, here is the fix...
*headdesk*
The fix was mentioned in reply #3, specifically the part that says, "Note that you're calling methods using the class name, *not* the reference variable."
~
hmm, you're right, completely did not read that....
i retract everything that i said
I know the problem now..
I wrote:
acc.getlevel
instead of:
Account.getlevel
thanks for the help
@OP: Please note that most of the confusion is a result of the non-standard way you name your classes and variables.
Names of classes are commonly written in CapitalizedCamelCase (like that), names of variables and methods are written in lowerCamelCase (like that). So instead of having a class acc and a variable Account, you should have a class Account and a variable acc. The methods in the Account class should be getXP, getLevel and so on (instead of all lowercase: getxp, getlevel).
You are accessing a method using class name "acc".
And this class not a static.
What u made mistake is
acc Account;
Account = new acc();
acc.getlevel();
acc.getxp();
this is not correct
try this
Account.getlevel();
Account getxp();
Here Account is a reference variable u have to access methods through using this "Account" reference variable.
It will work.Try this
public class main{
public static void main(String args[]) {
acc Account;
Account = new acc();
System.out.println("level: " + Account.getlevel());
System.out.println("xp: " + Account.getxp());
}
}
And you must carefull with java code conventions.
Class name must start with Capital letter.
you should not write like this "acc" Use Acc .
and also if u declare variables declare in small letters.You declare reference variable with capital letter don't do that.
You must use small letters like this "account"
Write program like this
public class Acc {
private int level;
private int xp;
private int hairstyle;
private int haircolor;
private int gender;
private int shirt;
private int legs;
private int nextlevel;
Acc() {
level = 1;
xp = 0;
hairstyle=1;
haircolor=1;
gender=1;
shirt=1;
legs=1;
nextlevel=1;
}
public void addxp(int xptoadd){
xp += xptoadd;
}
public int getxp() {
return xp;
}
public int getlevel() {
return level;
}
public void setlook(int genderchoose, int shirtchoose, int legschoose, inthaircolorchoose, int hairstylechoose){
gender = genderchoose;
shirt = shirtchoose;
legs = legschoose;
haircolor = haircolorchoose;
hairstyle = hairstylechoose;
}
public int gethairstyle() {
return hairstyle;
}
public int gethaircolor() {
return haircolor;
}
public int getgender() {
return gender;
}
public int getshirt() {
return shirt;
}
public int getlegs() {
return legs;
}
public int getnextlevelxp() {
if(level == 1) {
nextlevel=200;
}
return nextlevel;
}
}
=========================================
public class Main{
public static void main(String args[]) {
Acc account;
account = new Acc();
System.out.println("level: " + account.getlevel());
System.out.println("xp: " + account.getxp());
}
}
Reply #3 is mysteriously invisible.
~
> Reply #3 is mysteriously invisible.
>
> ~
You wrote it in invisible pixels.