Results 1 to 4 of 4
  1. #1
    infernocy is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default How to pass fields throw classes

    Hallo

    How can i pass a field from one class to another one . I have A depot class and a Vehicle class and i have to pass the depot name field from the depot class as the home depot field in the Vehicle class.

    thanks

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: How to pass fields throw classes

    Most people would adopt the use of what are called "Accessor" methods; methods that get and set a particular variable. In this manner, class A can use Class B's getVarA() method to get VarA from class B; getVarA() would be called a "getter" method. The similar patter is used for "setter" methods.

    While there is a place for these, they generally are indicative of a poor design. In the very simplistic case you are working with, I don't think using accessors is a problem.

    To read more about this, read Why getter and setter methods are evil

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default Re: How to pass fields throw classes

    Quote Originally Posted by infernocy View Post
    I have A depot class and a Vehicle class and i have to pass the depot name field from the depot class as the home depot field in the Vehicle class.
    A question for you: is each Vehicle associated with a Depot name ... or with a Depot?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    infernocy is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: How to pass fields throw classes

    Quote Originally Posted by DarrylBurke View Post
    A question for you: is each Vehicle associated with a Depot name ... or with a Depot?

    db
    there are two fields in the depot class - a collection of Vehicles and the depot name - for the depot name it says this - String , set in constructor - this also should be the string passed to a Vehicle for its home depot field - and for the home depot field in the vehicle it says String identifying the depot at which the vehicle is normally kept - set in constructor but can be changed

Similar Threads

  1. when is throw used in try block
    By Adithi in forum New To Java
    Replies: 5
    Last Post: 09-06-2011, 04:07 AM
  2. Replies: 2
    Last Post: 03-28-2011, 04:15 PM
  3. Throw and catches
    By cdawg_2010 in forum New To Java
    Replies: 3
    Last Post: 11-23-2010, 01:36 PM
  4. where to use throw and throws
    By javastuden in forum New To Java
    Replies: 4
    Last Post: 11-02-2010, 03:31 PM
  5. what exception to throw
    By DoolinDalton in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 03:45 PM

Posting Permissions

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