Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Why can't I see this string variable?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have two classes

ShowRoom:



and Car:



How come from ShowRoom I can see

public class Car {
   private
   String carColor;
   int doorCount;
   float engineSize;

but i cant see/use String carDescription;

unless I make it public.

 
Greenhorn
Posts: 5
VI Editor Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have private access modifier applied to 'carDescription' field.
 
Saloon Keeper
Posts: 6227
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what you wrote had you organized it properly. Only one field is marked 'private'.
 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:
 
Why are you passing information to the car method that the car already knows? It's like me telling you, "Hey, Eric, tell people that your name is Eric and that you're a guy." when I should just say "Hey, dude, please tell people your name and gender.

You can eliminate the parameters altogether and make the code simpler.

Think about that design some more. What's to keep someone from doing this:

or even

You wouldn't want that, would you?

Kidding aside, the design breaks the Car object's encapsulation. Knowledge about a Car should be kept inside the Car as much as possible. By making field values that should be private available to the public, you lose control of that information. In the real world, you'd be opening up to identity or information theft. Objects should guard their personal information jealously.


 
Eric Flynn
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"yo mama's booty" lmao

ok so you cant do like c++

private:
var
var

so am i correct in saying that no variables should be available to be manipulated from outside the class, it all needs to be done through methods

it was just a silly example i was experimenting on, nothing serious
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Flynn wrote:
ok so you cant do like c++

private:
var
var


No, you can't.

so am i correct in saying that no variables should be available to be manipulated from outside the class, it all needs to be done through methods


If you want to preserve an object's encapsulation, yes, that's correct. Access to object attributes should be strictly limited and if necessary, be done through accessor (getter) and mutator (setter) methods. Encapsulation is quite a lengthy topic of discussion and I'd encourage you to read what Joshua Bloch writes about it in his book, "Effective Java Programming" -- Edit: sorry, I think Joshua actually writes extensively about inheritance in his book.

Search for how to preserve object encapsulation
 
A feeble attempt to tell you about our stuff that makes us money
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!