Results 1 to 12 of 12
- 03-22-2011, 11:33 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Composition: Why is the compiler screaming
Why is my IDE refusing to accept this? I am trying to use composition here. Elsewhere in the code I have created a cheese object and it is its displayProductDetails method that I am calling.
Java Code:public String printDescription(int productIndex){ int y = productIndex; if(y == 0){ public String displayProductDetails(){ String x; x = [I]cheese[/I].displayProductDetails(); return x; } } }
- 03-22-2011, 11:43 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
did you name the instance variable that you got through composition cheese? Or did you name it something else?
Java Code:public class Composition{ Class1 cheese; //<--is this named cheese? public String printDescription(){ String x; x = cheese.displayProductDetails(); } }
- 03-22-2011, 11:50 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Yes I did name it cheese, and I have created this object in the same class as the code that is failing.Java Code:private Products cheese = new Products("Cheese", 2.33, "cheese001", "Sour Russian Goat Cheese", false, false);
- 03-22-2011, 11:52 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ill probably need to see more of the code, first, however; what compiler errors are you getting?
- 03-22-2011, 12:02 PM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
I am getting an illegal start of expression error.
- 03-22-2011, 12:04 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Can you paste the exact error(copy/paste) please?
- 03-22-2011, 12:09 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
your error is here. Think about it and see if you realize why.Java Code:public String displayProductDetails(){ String x; x = cheese.displayProductDetails(); return x; }
Look at the whole posted snippet in your original post, and read the part I just posted and you should be able to figure it out, if you can't let me know.
- 03-22-2011, 12:11 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 03-22-2011, 12:25 PM #9
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
@Sunde887
For fear of sounding stupid I will keep what I am thinking is the problem to myself. In fact my thoughts are not at all clear. Can you please let me know what is going on.
All my mind could think of was that there was no return to the first method. But I know that is probably not the reason.
And what of what Josah is saying as regards defining a method inside another method?
- 03-22-2011, 12:44 PM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What jos said was exactly what I was hinting at as well,
In your original snippet, you posted a method, where inside the method you tried defining a new method, and this causes an error, because you can't(as jos said), define a method inside other methods.
- 03-22-2011, 12:49 PM #11
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Thank you for your help. This is all quite frustrating but I will have to rethink this one way or the other.
Last edited by africanhacker; 03-22-2011 at 12:54 PM.
- 03-22-2011, 01:05 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
There's not much to think about; you tried to do this:
As I wrote before: you can't do that; make it look like this:Java Code:public class YourClass { public String printDescription(int productIndex){ public String displayProductDetails() { ... } } }
kind regards,Java Code:public class YourClass { public String printDescription(int productIndex){ ... } public String displayProductDetails() { ... } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Composition vs inheritance
By mindus in forum Advanced JavaReplies: 3Last Post: 09-27-2010, 07:41 AM -
composition Vs Inheritance
By mindus in forum New To JavaReplies: 1Last Post: 09-26-2010, 07:53 PM -
association vs composition vs aggreagation
By waqar100 in forum New To JavaReplies: 2Last Post: 06-10-2010, 10:17 PM -
I need some help with composition, i think
By Nerijus in forum New To JavaReplies: 2Last Post: 03-04-2010, 06:24 PM -
What is the Composition in Java
By Santoshbk in forum Advanced JavaReplies: 4Last Post: 04-10-2008, 11:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks