Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Basic question in Java Objects - Static

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I thought static method cannot access instance variables/methods.

In circle class PI is instance variable and in the "public static void main", main method, I created Object of circle and able to modify instance variable. Where did my understanding went wrong?
 
Marshal
Posts: 71752
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't confuse objects and static members/initialisers of a class. If they are static, they don't count as part of any objects.

The standard form of the main() method creates an object, yes, and having a reference to that object allows it to call instance methods, etc. It is better design to move your execution out of the main() method as soon as possible.

I can see two design errors in your Circle class. Please tell me what they are.
 
Dreke Droga
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

I can see two design errors in your Circle class. Please tell me what they are.



The "public double PI = 3.14;" should be "public static  final double". Value is constant and should not be changed by another object from Circle. So static final is needed. "METERS_PER_INCH" constant is redundant. Please let me know if there are more design errors.

I moved the Circle object initialization outside main method. The object variable was not accessible until I made the object variable static at declaration.
 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that was one error, but I didn't realise you wanted a constant. I didn't worry too much about inches; that isn's a serious error.

Dreke Droga wrote:. . . Please let me know if there are more design errors. . . .

Why does its area always come out as 0?

moved the Circle object initialization outside main method. . . . .

That's a new error Get out of the main() method and into an object as soon as possible.
 
Dreke Droga
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why does its area always come out as 0?



method action is not present. default value is zero for the data field radius. Calculate method is taking int value. I should make it this way.

 
Campbell Ritchie
Marshal
Posts: 71752
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dreke Droga wrote:. . . . I should make it this way. . . .

No, you shouldn't.
  • 1: setXXX() methods denote a mutable object, and it is best to reduce mutability as much as possible. Also, setXXX() and getXXX() methods may break encapsulation and therefore violate some of the principles of object‑orientation. (Old thread about setXXX() methods.)
  • 2: If you allow the field to retain its default value, you will have your object in an inconsistent state; there is no such thing a a circle with radius 0. You might have radius 0.000...0001 but not 0. Remember there is no way to “force” your users to call any setXXX() methods.
  • Notice I haven't told you the solution yet.
    Don't call the method Calculate() with a capital C. Say calculateArea() or similar instead.
    We havn't yet discussed the mistake about making any fields static.
     
    Dreke Droga
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:It is best to reduce mutability as much as possible.


    Took notes!

    Campbell Ritchie wrote: Also, setXXX() and getXXX() methods may break encapsulation and therefore violate some of the principles of object‑orientation. (Old thread about setXXX() methods.)[/list]


  • I'm still learning on objects and honestly, I gave a pass on Abstraction and encapsulation. Both seemed confusing. I felt both are same. But today I gave another reading, and I still feel both are same.  

    Campbell Ritchie wrote:2: If you allow the field to retain its default value, you will have your object in an inconsistent state; there is no such thing a a circle with radius 0. You might have radius 0.000...0001 but not 0. Remember there is no way to “force” your users to call any setXXX() methods.


    I couldn't find on references on leaving object values to defaults will leave object in inconsistent state. I would like to know more about this. Can you please give me any reference to this subject area?

    Campbell Ritchie wrote:Don't call the method Calculate() with a capital C. Say calculateArea() or similar instead.


    Ah.. the basics said this. I forgot. Below code after changing.



    But there's confusion. I think I need to start reading objects from concepts from beginning again.
     
    Campbell Ritchie
    Marshal
    Posts: 71752
    312
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dreke Droga wrote:. . .  Abstraction and encapsulation. . . . both are same.

    They aren't. A data abstraction is some representation of what the data are, without necessarily having any values. You have a radius in a circle, but you don't say what the radius is. When you specify a value for the radius you have changed from an abstraction to a concrete representation.
    Encapsulation means that the works of your representations are hidden from other code. Your objects have a public interface and everything else should be hidden.

    . . . . I think I need to start reading objects from concepts from beginning again.

    Which book are you using?

    In my opinion every class should be given a constructor. The invariants for a circle are that it has a certain value for its radius. That value is finite and > 0 i.e. positive. They should have told you already what the default values of fields are, but they are in the Java┬« Language Specification (=JLS). Note that local variables don't have default values.
     
    Dreke Droga
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Which book are you using?


    I don't have any book. I have list of topics gathered in order to learn. I downloaded it from some site. And then for the topic I saw youtube video or tutorialpoint etc. I had pdf of Java by Herbert Schildt. I read through couple of chapters and lost interest. Not that it's fault of book. But I found it difficult understand with my minimal english skills and in bonus - I'm a slow learner Please let me know if you have suggestions.
     
    Dreke Droga
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:
    Which book are you using?



    Do you have any suggestions for me? I'm beginner for coding.
     
    Campbell Ritchie
    Marshal
    Posts: 71752
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sierra and Bates' Head First Java might be old, but I think it is better than Schildt. Get a second‑hand copy and don't pay the “new” price.
     
    Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic