• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with a static method in a class

 
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic