Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with a static method in a class  RSS feed

 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to resolve an exercise. The track is "Write a program that demonstrates that, no matter how many objects you create of a particular class, there is only one instance of a particular static field in that class."

So I wrote a program that doesnt' work (In any case I hope I understand the meaning of the exercise).

This is the program:



When I compile i've error with d1/d2/d3-symbol not found.

If i delete the method class, here the code..:


..the program works perfectly. Why?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:If i delete the method class, here the code..
..the program works perfectly. Why?

Because the problem is in your Stampa class.

The fact is that static methods are NOT usually what you want (at least at the moment). As soon as you've created an object (which you have), you can run instance methods, which are much more flexible.

You might want to look at the MainIsAPain page for more information.

Winston
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Ivan Addeo wrote:If i delete the method class, here the code..
..the program works perfectly. Why?

Because the problem is in your Stampa class.

The fact is that static methods are NOT usually what you want (at least at the moment). As soon as you've created an object (which you have), you can run instance methods, which are much more flexible.

You might want to look at the MainIsAPain page for more information.

Winston


Thanks for help! I've read the page you've linked. But how can i use an istance method instead of static method in this program for solve the problem? A non-static method can't access static fields, but the exercise is about static, and String is static.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:A non-static method can't access static fields, ...

That is not true. A non-static method can access static fields.

The reverse, however, is true: a static method cannot access non-static fields.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can see, this has nothing to do with static and non-static methods.
The Stampa class does not contain variables called d1, d2 and d3, so the compiler complains. This will happen whether the stampa() method is static or not.
In your second piece of code, you have moved the print statements to the main method of the Dimostra class and variables called d1, d2 and d3 do exist there.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:Thanks for help! I've read the page you've linked. But how can i use an instance method instead of static method in this program for solve the problem?

Read Stuart's post. He speaketh sense.

but the exercise is about static

And how are we supposed to know that? We're good, but we're not mind-readers.

and String is static.

No. 'String' is a type. It can't be static unless the variable (field) that you've assigned it to is static; and the only place you've done that is with the variable Stringa.s.

Winston
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:As far as I can see, this has nothing to do with static and non-static methods.
The Stampa class does not contain variables called d1, d2 and d3, so the compiler complains. This will happen whether the stampa() method is static or not.
In your second piece of code, you have moved the print statements to the main method of the Dimostra class and variables called d1, d2 and d3 do exist there.


Ok, i've modified the code in this way and it works:



But why d1/d2/d3 exist only when i put them?


Winston Gutkowski wrote:And how are we supposed to know that? We're good, but we're not mind-readers.

But i've explained the exercise's track in the firs post


Winston Gutkowski wrote:No. 'String' is a type. It can't be static unless the variable (field) that you've assigned it to is static; and the only place you've done that is with the variable Stringa.s.

Can you explain more clearly?


Thanks at all for your help!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!