/    Sign up×
Community /Pin to ProfileBookmark

newbie — variables question

Ok I just started learning Java. I’m using this tutorial: [url]http://java.about.com/od/beginningjava/a/beginjavatutor.htm[/url] and using Eclipse as my IDE. Now, I tried this simple code:

[code]
public class HelloWorld {

private int somevar = 5;

public static void main(String[] args) {
System.out.print(somevar);
}

}
[/code]

but I get this error:

[code]
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static field somevar
[/code]

it works if I delcare somevar like this: “private static int somevar = 5;”. But I don’t want it to be static. Help please ?

to post a comment
Java

5 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleNov 20.2007 — It is because you are trying to access it from a static method. You can do one of two things here. One is to declare your variable in the method. Two, you can make the variable static. Why do you not want it to be static? If that is really the case then you most likely should not be declaring this variable in your entrypoint class.
Copy linkTweet thisAlerts:
@ZnupiauthorNov 20.2007 — Well, I'm very familiar with PHP and I'm trying to translate this:
[code=php]
class HelloWorld {
var $myvar;
function HelloWorld($newvar) {
$this->myvar = $newvar;
}
function printVar() {
echo $this->myvar;
}
}
$hc1 = new HelloWorld(5);
$hc2 = new HelloWorld(2);
$hc1->printVar();
$hc2->printVar();
[/code]

into Java, so I tried this:

[b]HelloWorld.java[/b]
<i>
</i>import "HelperClass.java";

public class HelloWorld {

<i> </i>public static void main(String[] args) { // Line 5 :(

<i> </i> HelperClass hc1 = new HelperClass(5);
<i> </i> HelperClass hc2 = new HelperClass(2);
<i> </i> hc1.printVar();
<i> </i> hc2.printVar();

<i> </i>}

}

[b]HelperClass.java[/b]
<i>
</i>
public class HelperClass {

<i> </i>private int myvar;

<i> </i>public HelperClass(int newvar) {
<i> </i> myvar = newvar;
<i> </i>}

<i> </i>public void printVar() {
<i> </i> System.out.print(myvar + "n");
<i> </i>}

}


But the compiler throws this error:
xception in thread "main" java.lang.Error: Unresolved compilation problem:

<i> </i>at HelloWorld.main(HelloWorld.java:5)

And also eclipse says there's an error on line one in HelloWorld.java:
Syntax error on token ""HelperClass.java"", Identifier expected

It seems I'm not really a fast learner ? please help, I don't understand why this isn't working ?
Copy linkTweet thisAlerts:
@jasonahouleNov 20.2007 — In Java you do not need the .java on your import. In fact, you absolutely need to get rid of because the compiled code will reference HelperClass.class. Also, there should not be any quotes around the imported file and it should be prefixed by the package unless it lives in the same package which in that case you do not need to import it at all.
Copy linkTweet thisAlerts:
@ZnupiauthorNov 20.2007 — Thank you. It works now ?
Copy linkTweet thisAlerts:
@jasonahouleNov 20.2007 — No problem. Java can be tough to grasp at first. I, like you, came from a PHP background so I know what you are going through.
×

Success!

Help @Znupi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...