0

This might seem elementary to many of you but ... when I declare a sentence like this:-

 Room room = findRoom(nbBeds, date_Entrance, date_Exit);

What does room hold ? does it hold the output of findroom ? what is this called? what should I read to understand this concept of creating a variable type class ?

2
  • although its pretty much sure that it will return Room object but stil could you show us the definition of findRoom(nbBeds, date_Entrance, date_Exit) ? Commented Feb 4, 2013 at 15:38
  • private Room findRoom(int nbBeds, String date_entree, String date_sortiee) { Room rAReserver=null; { Iterator i = roomList.iterator(); if (!roomList.isEmpty()){ while(i.hasNext()){ Room r = (Room)i.next(); if (!this.isNotAvailable(date_entree, date_sortiee)) { rAReserver= r; Commented Feb 4, 2013 at 15:40

4 Answers 4

3

your findRoom(nbBeds, date_Entrance, date_Exit); should return an object with type Room.

your room references that object. Note that the object can be null.

if the "output" you meant in your question is the outputs (by System.out.print for example) to console. The outputs have nothing to do with returned value. You can output anything, but you have to return a Room type object. Otherwise you got compilation error on that line.

Sign up to request clarification or add additional context in comments.

4 Comments

If I type Room room without the rest of the sentence, does that mean I am creating a variable type Room which can access all the instance variables inside class Room ?
you need to initialize that room instance by doing something like Room room = new Room(); (in most cases) - provided Room class is not abstract
Think of it this way: a class definition is a blueprint, an instance is the house you build according to the blueprint, using the colors, materials, etc. that are allowed by the blueprint. Just looking at the blueprint doesn't tell you whether the people who built a house decided to go with red or green coloring for the walls.
@user1860176 if you do Room room; You declare a variable room, with type Room, but it is not initialized, you cannot use it directly. if you initialize it by either new Room() or findRoom(...), then yes, you could access all(if it is allowed) instance attributes. If you say Room room=null; it is initialized, but is null. in this way you cannot access room's attributes. I suggest you reading some Object-Oriented Programming articles, you will get clear picture.
1

if findRoom(nbBeds, date_Entrance, date_Exit) returns an Room Object then room(reference variable) just holds the address to the Object returned by findroom.

Comments

1

That should have been clear by seeing return type of findRoom(nbBeds, date_Entrance, date_Exit) method

and as far as your code shows, it seems it returns an Room object that will be assigned to room

EDIT: as you posted in comment findRoom method is defined as :

  • private Room findRoom(.....){ .... }

(you see this Room code after private - it shows that return type of method will be of Room type

Comments

0

The variable room references an instance of Room (room contains the address to the Heap memory that contains the object).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.