• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to pass a parameter that is of type class into another class's constructor?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im very new to programming and im super confused about something.
basically, i have 4 classes. 1 2 3 and 4. 4 can be called by 2 and 3. 1 can call on both 2 and 3 and 2 can also call on 1. i feel like thats really bad explaining but its the only way i can explain without posting all the code on(i dont even know how)
i made a 2 fields in class 1 of type 2 and of type 3.  the problem i have is when i try to call a method on the type 2 field that is from class 2, that method also then asks for a parameter of type 4 and i dont know what im supposed to input as this is all in the constructor.

i get the    method in class cannot be applied to given types     error message.

any help would be appreciated.

 
Marshal
Posts: 28134
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

new persons wrote:i feel like thats really bad explaining



Yes, your instinct is correct. It is really bad explaining. But don't let that deter you, you can do better.

but its the only way i can explain without posting all the code on(i dont even know how)



Show us the code. Do you think there's too much code and we're going to be put off by having to scroll forever? That's quite possible. But your question is just about how to declare parameters, so we don't really need to see what's inside the methods and constructors. You could leave that out.

Or are you concerned about how to get the code from your machine to the forum? Copy it from your code editor and paste it in the box you're posting in. If necessary, copy it to somewhere else on your machine and delete irrelevant code before pasting what's left into the forum's post box. Don't worry too much about whether it looks good, anything is better than what we have so far and we're all friendly and used to beginners here.

And, welcome to the Ranch!
 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the thing i was told to do was only to write code for this class im about to post, it is pretty empty atm i have to fill stuff out



i just copied the naming of the fields and the constructor part.  what im struggling with is the last line. it asks for type Property but i have no clue what to write.  the other classes i added below.









that is the first class, the one i said was 4 if you managed to decrypt my message.




 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



this was 3.

 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this was 2.

 
Paul Clapham
Marshal
Posts: 28134
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john kaine wrote:i get the    method in class cannot be applied to given types     error message.



Okay. Then it would be useful for us to know exactly which line of code produced that message.

Maybe you get it more than once? Then you could tell us about every line of code which produces it.
 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was struggling with the last line on the 1st message that said gui.showProperty().  i spent about 3 hours on that one line before realising that i had to enter the line before it (the portfolio.getProperty(0) line) as the paramter for the gui.showProperty().  as painful as it was to waste so much time on something so easy you were probably cringing actually gave me suuuuch a huge smile.
 
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please look at our link about the code button; it isn't obvious at first how to use it, but you put the code between the [‍code=java] part and the [‍/code] part. Since you are new I have corrected it.
If you hit problems, please ask for help early; it is much easier to help if you have written only a little code. The small parts I have read look good, but why are you matching a field description with a constructor parameter name?

Actually passing parameters to constructors is the same whether they are reference types or primitives. Let's have a Kettle which contains water when new, so its constructor might look like this:-. . . and you can call it like this, assuming the Water object already exists:-Beware: Strings are immutable and ints are primitives, so there is no need for a defensive copy of them, but Water might be mutable, so take a defensive copy OR tease the object apart into primitives and immutable objects, to maintain the integrity of your object. Your property class doesn't (I think) need any defensive copying.
Two old posts about defensive copies: 1 2.
 
Paul Clapham
Marshal
Posts: 28134
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john kaine wrote:i was struggling with the last line on the 1st message that said gui.showProperty().  i spent about 3 hours on that one line before realising that i had to enter the line before it (the portfolio.getProperty(0) line) as the paramter for the gui.showProperty().  as painful as it was to waste so much time on something so easy you were probably cringing actually gave me suuuuch a huge smile.



Yeah, we've all been there. I remember... but everyone has their own anecdotes. So, for you it's on to the next tar pit!
 
Campbell Ritchie
Marshal
Posts: 79039
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . Yeah, we've all been there. I remember... . . .

I once spent two hours wondering why my method called throw() wouldn't compile.

on to the next tar pit!

You're such an incurable optimist, aren't you, Paul
 
john kaine
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well it looks like i have a lot to learn :p
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic