• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

declaring/defining variables - style and/or substance?

 
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many multiclass programs make use of the same variables in multiple classes; e.g. length and height in various geometric structures.  In general, there are 3 ways to make "copies" of the variables - pass them as parameters, obtain them via getX() methods, and (re)declare them within each class.  I can think of examples where one way has a clear advantage, and examples where I see no clear advantage.  Is there a widely accepted rule of thumb about this?

I know subclasses can be a good option in some situations, but that's not what I'm considering at the moment.

I'm most interested in the efficiency of the various methods (the substance, if you will) rather than the style, but style does matter when it comes to code readability and/or maintenance.  Any advice?
 
Sheriff
Posts: 24632
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's none of those. It's all about the design of the classes. If a class needs a length variable then it should have one, end of story. Putting that variable somewhere else because somebody said "efficiency" is almost always a bad idea.
 
Al Davis
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously if the class needs the variable it should (must) get it.  My question is asking whether there is a "best" way to go about putting it there, assuming that there are multiple ways to do so..
 
Marshal
Posts: 65365
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It should normally be an instance field. Passing arguments and getXXX() methods are ways to get such information into and out of an object. The three things are different. If there are three classes each with a length field, don't go thinking that is some sort of re‑declaration. They are separate fields. But make there isn't a field of the same name in a superclass; that would be hiding of fields and can cause no end of confusion.
Don't go saying that “efficiency” is “substance” and class design is  “style”. If somebody has told you that, they are labouring under a severe misconception about programming concepts. Speed of execution is usually the last consideration in your mind when you design a program. No, memory consumption probably ranks even lower than speed much of the time.
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Davis wrote:Many multiclass programs make use of the same variables in multiple classes; e.g. length and height in various geometric structures.  In general, there are 3 ways to make "copies" of the variables - pass them as parameters, obtain them via getX() methods, and (re)declare them within each class.  I can think of examples where one way has a clear advantage, and examples where I see no clear advantage.  Is there a widely accepted rule of thumb about this?

... Any advice?



Advice: If you want clear and specific answers, ask clear and specific questions.

Instead of saying "I can think of examples..." and then not giving one, you can give at least one specific example so that people don't have to guess what you have in mind. People, especially programmers in particular like many of us are, are not very good at mind reading. In fact, we suck as mind readers, so try to give as much context to your question as you can to keep people from making their own assumptions.

If there's a widely accepted rule of thumb that comes to mind from what you said, it's this: Do what makes sense. If that answer is too vague, that's only because I don't want to make any assumptions about what you had in mind when you framed your vague question.

 
Paul Clapham
Sheriff
Posts: 24632
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Davis wrote:Obviously if the class needs the variable it should (must) get it.  My question is asking whether there is a "best" way to go about putting it there, assuming that there are multiple ways to do so..



This seems to be not a question about whether a class should have a variable, but a question about how the variable should be initialized. Your way of writing the question is hard to follow, because for me the way to "put" a variable in a class is to declare it as an instance variable of the class. There's only one way to do that.

Now, if you're asking for the best way to initialize a variable (or to "put" a value in a variable, if you like), the answer (as usual) is "It depends". In particular it depends on the design of the class and the design of the application which is using the class.

Let me just comment that beginners are usually led to believe that a Java program is a collection of code fragments which have to be persuaded to work together, just because they are taught the mechanics of the languge and not object-oriented design. In fact a real Java program is more like a collection of objects which work together.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!