• 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

The book Learning Java need some clarity

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI People,
I have not run this code yet.



I am going to assume that line 1. is setting up the class.
Line 2. is the start of the program, I think. Or it is the beginning of the class. Pick one please.
Line 3. is very much like php. It calls the class into action, instantiation, I think.
Line 4. just prints out Apple a1
Line 5. Here is where I am getting confused. How does this work a1.printDetails ? I don't see anything to tell me what is going to print out. If I did this in php I would have to have printDetails some where else.
line 7. is an assignment that puts 10.0f a floating point number
line 8. is another assingment
I am going to skip the obvious
line 11. again a1.printDetails where, what, how does this work.


Thanks for your help. I tried to be neat.
Mark


 
Mark Roberge
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never mind I found the print details section.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take some time to learn how to use code tags. A helpful tutorial could be read here <--- click it.

Another note on the same, once you go through the tutorial, and learn how to use code tags, you'll find, that you don't need to spend time and annotate each line with line numbers, added code tags will provide you that functionality out of the box.

Example code surrounded with code tags.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Roberge wrote:never mind

You should mind; the quality of your learning is important.

I found the print details section.

What did that say? How did it differ from what you wrote? What do you think I thought was wrong with lines 7‑9?
 
Mark Roberge
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, why is it in Java they us a1.mass for example, to make a variable?  I have studied PHP and variables use a $ to distinguish a variable from other items in the code.

Something else that I have noticed is that in Java all of the classes are in line with the rest of the code. In php I can write all of the classes I want store them in my includes folder and when I call on that file all of the classes are brought into the main file and ready to be used. I see java as somewhat confusing on this point.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Roberge wrote:OK, why is it in Java they us a1.mass for example, to make a variable?

You would only write a1.mass if you are ignoring the principle of hiding implementation details. That might be correct Java® syntax, but it is poor object‑orientation.

I have studied PHP . . .

If you go round thinking that different languages do things the same way as each other, you can get into no end of confusion. The same applies to natural languages; for example sympathique (French), simpatico (Italian), and sympatisch (German) mean more or less the same thing. But it is completely different from the English word sympathetic's meaning. Java® doesn't use a $ operator,  and it doesn't use an include folder. But it has other ways to include outside classes. Please explain what you meant by, “in line”. I suspect there is something about packages and modules you haven't learnt yet.
 
Mark Roberge
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By inline I am talking about the flow of the code. From top to bottom.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't what inline means, but don't other languages run from top to bottom too?
 
Mark Roberge
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am most familiar with PHP. Classes in PHP can be placed in an include file and called into the program with an include or require. And yes I am referring to top to bottom.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equivalent to include is probably an import declaration.
Did you work out why I wasn't happy with lines 7‑9?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Roberge wrote:I am most familiar with PHP. Classes in PHP can be placed in an include file and called into the program with an include or require. And yes I am referring to top to bottom.



What you refer to as "inlining" is probably for expediency in the book. If the code is all in one listing and Apple is not declared as public, then it's to avoid doing what I show below.

Normally, you would keep the code for each significant class in a separate source file (also called a compilation unit) and just import the class wherever you need it. If two classes are in the same package, there's no need to explicitly declare them with an import statement. In Java, the package the class is in is reflected in the file system directory structure.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Campbell's point, it's not good form to expose instance variables to direct access and manipulation, especially if there's code in the class that uses it in interesting ways. You want to be fully aware and in control of any changes that are made to an object's internal state. Allowing code external to a class to directly manipulate its state variables is bad practice and can be the source of hard-to-track bugs. So you typically encapsulate instance variables by making them private and providing get/set methods, also known as accessor/mutator methods, for external code to use.

The printDetails method could also be replaced by simply overriding the toString() method inherited from Object.

With this implementation, the original code would change to:

Unfortunately, this contributes to the verbosity of Java code. In contrast, Kotlin, another JVM language, the same class can simply be declared like this:

That's it. Just one line of Kotlin equivalent to many lines of Java.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am always sceptical about setXXX() methods.
Another thing I wasn't happy about was the use of 10.0f for the mass; I dislike the float datatype intensely.
reply
    Bookmark Topic Watch Topic
  • New Topic