Results 1 to 12 of 12
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default 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;
    	                }
    	            
    	            
    	        }
    	       
    	   }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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();
      }
    }

  3. #3
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Java Code:
    private Products cheese = new Products("Cheese", 2.33, "cheese001", "Sour Russian Goat Cheese", false, false);
    Yes I did name it cheese, and I have created this object in the same class as the code that is failing.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ill probably need to see more of the code, first, however; what compiler errors are you getting?

  5. #5
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I am getting an illegal start of expression error.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Can you paste the exact error(copy/paste) please?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    public String displayProductDetails(){ 
                                String x;
    	                    x = cheese.displayProductDetails(); 
    	                    return x;
    	                }
    your error is here. Think about it and see if you realize why.

    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.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by africanhacker View Post
    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;
    	                }
    	            
    	            
    	        }
    	       
    	   }
    You're trying to define method displayProductDetails() inside the body of another method. Java doesn't support that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    @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?

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  11. #11
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    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.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by africanhacker View Post
    Thank you for your help. This is all quite frustrating but I will have to rethink this one way or the other.
    There's not much to think about; you tried to do this:

    Java Code:
    public class YourClass {
       public String printDescription(int productIndex){
          public String displayProductDetails() { ... }
       }
    }
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Composition vs inheritance
    By mindus in forum Advanced Java
    Replies: 3
    Last Post: 09-27-2010, 07:41 AM
  2. composition Vs Inheritance
    By mindus in forum New To Java
    Replies: 1
    Last Post: 09-26-2010, 07:53 PM
  3. association vs composition vs aggreagation
    By waqar100 in forum New To Java
    Replies: 2
    Last Post: 06-10-2010, 10:17 PM
  4. I need some help with composition, i think
    By Nerijus in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 06:24 PM
  5. What is the Composition in Java
    By Santoshbk in forum Advanced Java
    Replies: 4
    Last Post: 04-10-2008, 11:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •