• Post Reply Bookmark Topic Watch Topic
  • New Topic

What can I use in a constructor and when should I create new classes?  RSS feed

 
Kevin Zhu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know whether it's common practice to use more than just field variables? I just started learning java today and I read about private variables within a class (not the main class -- could the main class even have private variables?). I read that private is used to keep others from changing it outside of the class, so I figured that maybe one uses is keeping the variable within a certain range. Hence, if you look at my constructor for Point, it has a conditional case, where one of them spits out a string. Also, I don't have anything my in constructor for my Points class (last box). Is that a problem? Could I also have somehow combined these classes? (I'm not even too sure when I need to create a class. It just seemed like a good idea to create two, or rather, it was easier for me.)

Goal: To find the distance between two points -- I just made it up.

I did not do very well in my first computer science course so I would believe that there are a lot of things that I could simplify in my code. I'd appreciate it if anyone can give me useful tips.







EDIT: Here's what I get when I run the program.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First: Welcome to JavaRanch, Kevin.

Kevin Zhu wrote:Does anyone know whether it's common practice to use more than just field variables?

Such as what?

I just started learning java today and I read about private variables within a class (not the main class -- could the main class even have private variables?). I read that private is used to keep others from changing it outside of the class...

Today? Then, let me say, what you've done is truly remarkable. Many beginners don't get the message about private variables until many weeks into their classes.

...so I figured that maybe one uses is keeping the variable within a certain range.

No. The primary reason is to keep control. If you let anyone change internal variables of your class directly, then you can never be sure if it holds the same values from one moment to the next, or if they're even set to valid values or not.

Hence, if you look at my constructor for Point, it has a conditional case, where one of them spits out a string.

Yup. Noticed that. Except that your conditional check is missing something quite important - Hint: what would happen if you wrote new Point(-73246, -8127374)?

Also, I don't have anything my in constructor for my Points class (last box). Is that a problem?

Not necessarily, but
(a) It's very good that you're thinking about things like this.
(b) In your case, it probably indicates that your distance() method declaration is wrong. What about instead providing a constructor that takes the two Points you want to work with, and then declaring your distance() method just like getCoord() (ie, no arguments)?

Could I also have somehow combined these classes? (I'm not even too sure when I need to create a class. It just seemed like a good idea to create two, or rather, it was easier for me.)

And that's just fine. For now, gong with your 'gut' is probably no bad thing; you'll soon find out when it's wrong. However, the answer to your question is (and almost always is): yes, you could have made distance(...) a static method in your Point class.

That said, it's probably better to err on the side of too many classes rather than too few - especially at this stage. You wouldn't believe how many beginners don't create classes for weeks and simply cram all their logic into main() (a VERY bad practise, BTW). You're well ahead of the game.

I did not do very well in my first computer science course so I would believe that there are a lot of things that I could simplify in my code. I'd appreciate it if anyone can give me useful tips.

Well, I can't see anything too terrible, but I would definitely change that variable 'l' to 'limit'; not least because 'l' looks very much like a '1' (it fooled me for a minute or two ).

Good, descriptive names (within reason) are really, really important.

If I see anything else glaring I'll let you know. But for one day? WELL DONE.

Winston

PS: Please DontWriteLongLines. It makes your thread quite hard to read. I've broken up the really long ones in your code for you this time, but please remember:
80 characters max.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kevin!

My little experience (over 2 years) tells me that :

Java Object - is such a thing that executes methods you call on it. Object can do some operations. Those operations are methods.

Java class - is a text composed by developer according to some rules. That text is used by JVM to crete objects and call methods on them.

Object exists to encapsulate certain methods! To peform methods an Object needs to keep some values(state), for this object uses variables.
Also object needs to get into some initial state before any method is called. This initals state is set via constructor.

To my mind, it is good when your java object represents corresponding object from real world.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:
Java class - is a text composed by developer according to some rules. That text is used by JVM to ...


You might have mixed up things here.

Well, the developer typically writes the source code (text indeed). the compiler compiles it in to the class files with the byte code, which is then used by the JVM.

This should be clear after the very first lessons in Java.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:Object exists to encapsulate certain methods! To peform methods an Object needs to keep some values(state), for this object uses variables.

Nothing particularly wrong with anything you say, but did you read Kevin's post? He just started learning today, so perhaps a little early to start getting too heavy into theory.

Winston
 
Kevin Zhu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for the replies, they all helped me in some way (especially yours Winston =P).

Kevin Zhu wrote:
Does anyone know whether it's common practice to use more than just field variables?

Such as what?


I was actually referring to the initialized variables in the constructors. For example, the conditional. In the java tutorials I've only seen variables initialized (by the constructor) for when an object is created.

Object exists to encapsulate certain methods! To peform methods an Object needs to keep some values(state), for this object uses variables.


I stopped reading after classes, what's next was Objects. I guess I should have finished that too. Thanks for the heads up though!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!