Graham Sills wrote:Q1) Why can't I access the car1 object contents? See commented code below?
Q2) If I have 3 Classes... ClassA, ClassB, & ClassC.
In ClassA if I create an object of ClassB, can I access the ClassB object from ClassC? I've been trying to do this but no luck. If it is possible to do so how do I need to do it?
Thanks.
Graham Sills wrote:Yes the is correct in my main program. I just edited it for this post.
The two classes are in difference files. And I have the main method in another class which calls the Reservation class.
You say "within your Reservation class you will not be able to access your Car instance variable like you are using it. "
1) Why?
2) What will I need to do so I can access the car instance variable?
this is quite not clear.can you please show us your code so that we will know what actually you have done.Graham Sills wrote:Q2) If I have 3 Classes... ClassA, ClassB, & ClassC.
In ClassA if I create an object of ClassB, can I access the ClassB object from ClassC? I've been trying to do this but no luck. If it is possible to do so how do I need to do it?
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Graham Sills wrote:The program is a car rental system. There are some errors because I've extracted the code and modified things a little so I can post something concise and easier to understand here. The mistakes called out here are not in my actual files, I just didn't edit my original post correctly.
OK someone mentioned I cannot access the car1 object because I have created it in the Constructor. OK then, however if I create it outside of the Constructor I still cannot access the object contents. Why? The error I get is when I enter the dot (car1.). I can't access anything in the car1 object.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
praveen kumaar wrote:you have done:
His Reservation class doesn't have such Constructor, he may find it difficult using that.
In as much as I view this constructor as a good one, without showing him how to use it, he might confuse the OP more.
Just saying though.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
praveen kumaar wrote:@timothy,apologies for not mentioning your name in quotes.
Please only post the exact code you are using in the code tags. The comment makes the line too long and the whole discussion becomes illegible to anybody with a small screen, e.g. the mobile view. Please put that sort of thing in the text: “There is an error in line 22 ...” You shlould also post the exact error message. We ahve seen those messages before and we know what they mean; the more details you give us, the faster we can help.Graham Sills wrote:. . . . . .
I am finding that difficult to understand. Does it mean that you think there is a statement outside a method, etc. I have moved the code tags because the comment shouldn't be inside the tags.praveen kumaar wrote:you have done:
--->here you may have got error identifier expected.it means your code needs some kind of indentifier
so that during runtime jvm can identify your code.identifier can be-static initializer,methods,
constructor,instance initializer...
Why are you setting the make of a Car object inside the Reservation constructor? The Car object ought to have a make already, and setting make is a matter for the Car class, not any other classes.Do some chages: ...
Kind Regards,
Praveen.
Campbell wrote:I am finding that difficult to understand. Does it mean that you think there is a statement outside a method, etc. I have moved the code tags because the comment shouldn't be inside the tags.
i just have think about it and found it worth doing such changes.Campbell wrote:Why are you setting the make of a Car object inside the Reservation constructor? The Car object ought to have a make already, and setting make is a matter for the Car class, not any other classes.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Timothy posted this:
Campbell wrote:
Why are you setting the make of a Car object inside the Reservation constructor? The Car object ought to have a make already, and setting make is a matter for the Car class, not any other classes.
Graham Sills wrote:
It works but why does it work? The only difference between that and my original is that the object reference variable was declared the constructor. OK but I don't understand why it needs to be like that? I guess I need to understand why my original code did not work and how this option works?
The error I was getting was "Cannot resolve symbol 'car1'".
Mobile app development and web development
praveen kumaar said:
why do you think it should have a setter.practically when a car is designed it was given a name and that name remains the same,though in future release its new model may come but in that case the all new object is used.
Timothy wrote:
You can't just pulled that up in a class. Just unclad without no method or a block.
And I thought, someone also mentioned it that you should initialize your class variables in your constructor. Which is a good practice.
Try the option I gave you and see what you have then let us talk about other part of your code.
praveen kumaar said:
You are not initializing the variables in your constructor rather you have a setter to do it,it is a very bad practice.
Hardik Jani said:
Right now I can not test your code but want to ask where is your Main class?
Graham Sills wrote:
1) I still do not understand why the following code does not work. The car1 object is created as an instance variable. So why can't I set the contents in the line below it?
The JLS wrote:ClassDeclaration:
...
NormalClassDeclaration:
... ClassBody
ClassBody:
{ {ClassBodyDeclaration} }
Graham Sills wrote:
You are not initializing the variables in your constructor rather you have a setter to do it,it is a very bad practice.
2) Is this always recommended? To initialize variables in the constructor? Also, if you do not use a constructor it is always recommended to initialize the variables as part of declaration? Also, I know if you don't provide a constructor Java will create one for you anyway but is it recommended to always provide a constructor as part of Java programming standard practice?
Graham Sills wrote:3) I'm not sure how to manage the creation of Car objects for the Reservation to be able to access them. The Reservation class needs to be able to know what cars are available from the inventory, and they will also need to display the specific Car object variables such as CarMake, CarModel, etc.
(Sorry for delay in replying; have been busy with gunpowder and trying to get prunings to burn in the rain. Both worked nicely, eventually )Graham Sills wrote:. . . I plan to (later . . .
Graham Sills wrote:3) I'm not sure how to manage the creation of Car objects for the Reservation to be able to access them. The Reservation class needs to be able to know what cars are available from the inventory, and they will also need to display the specific Car object variables such as CarMake, CarModel, etc.
Graham Sills wrote:
Most of my logic though is going to be in the Reservation class. Here I will have methods containing logic for:
SearchForCars
MakeReservation
EstimateReservation
SearchExistingBooking
ReturnCar
Junilu Lacar
Line 5 - This is the line you are confused about. It does not match anything that the JLS lists as being something that can be contained in a legal ClassBodyDeclaration.
3) I'm not sure how to manage the creation of Car objects for the Reservation to be able to access them. The Reservation class needs to be able to know what cars are available from the inventory, and they will also need to display the specific Car object variables such as CarMake, CarModel, etc.
For example I will have to set multiple variable data for the car object for each car. Example:
Currently I have a Car class which every car will have an object, and I am creating the Car objects in the Reservation class. But I am thinking of creating a third class to sit in the middle called CarInventory. And I can move all the creation of the Car objects from the Reservation class to the CarInventory. But if I do that, then how can I access the Car object details (e.g CarID, CarModel, etc) from the Reservation class? Because the Reservation class will have one object of CarInventory and will not have direct access to the individual Car objects. In the Reservation class I cannot say:
?
1
CarInventoryInstance.Car1.getCarType()
Can I?
Does Java have to have direct access to an object? I mean does the object declaration have to be in the same class that I want to access it? My example above in what I want to do is basically saying that I want to access Car object information from the Reservation class through/via the CarInventory class. Is that possible? If not then how should I be doing it?
Junilu Lacar asks:
Does a Reservation really need to "know what cars are available from the inventory"?
Campbell Ritchie wrote:
Don't plan to do things later
Graham Sills wrote:
Line 5 - This is the line you are confused about. It does not match anything that the JLS lists as being something that can be contained in a legal ClassBodyDeclaration.
This doesn't make sense to me because there are many things than can be done in a class body apart from 'ClassBodyDeclaration'. Line 5 is not part of ClassBodyDeclaration,I know, and I'm not trying to declare anything in line 5. That is already in line 4. Line 5 is invoking a method but are you telling me Java does not allow invoking a method if it is not stated within another method?
I have spent some time in the JLS and it's not very informative for a beginner, quite convoluted.
Can someone try explain to me in layman's terms why line 5 is not allowed?
Graham Sills wrote:And this only applies to objects? Because for primitive types it is possible to use various expressions and statements.
Is there anything apart from object declarations that IS allowed for object reference types (outside of methods)?
Graham Sills wrote:
Line 5 - This is the line you are confused about. It does not match anything that the JLS lists as being something that can be contained in a legal ClassBodyDeclaration.
This doesn't make sense to me because there are many things than can be done in a class body apart from 'ClassBodyDeclaration'. Line 5 is not part of ClassBodyDeclaration,I know,
Sergiu Dobozi wrote:I'll explain this in layman terms (but I'm not a programmer). If you create a class and you want to have a command or manipulation of a specific created object, you will need to place that into either a constructor or method...anything outside of that will simply not run, thus it will simply not exist. And it's illegal and apparently it's illegal not to exist in Java
Junilu Lacar wrote:
Good try.
However, you left out line 5 being legal in an instance initializer block. Besides, in this context, "class", "object", "constructor", and "method" are hardly layman's terms, if you ask me.
Sergiu Dobozi wrote:But I think we also left out static initialization blocks, no?
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|