Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Noob question: Getting value from another class

    Heya,

    Sorry for this very basic question but.

    I have a driver class, which uses 3 other classes to create the objects. (Simulating a trucking company, driver class, vehicle class, route class and trucking company class)


    In the route class how would i get a value from a created vehicle?

    Each vehicle is rated with a cost per hour value and i need to add the total of all the vehicles on that route.

    thanks

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    It may be easier to answer your question if you post your code so we can use that as an example and have it be in context with your application. Here is a smaller example to try and illustrate it though.

    We can start by defining two classes, Example1 and Example2. Example 2 will access data from Example1.

    PHP Code:
    public class Example1
    {
        private 
    String data1 "this is my test data";

        public 
    String getData1()
        {
            return 
    data1;
        }

    PHP Code:
    public class Example2
    {
        public static 
    void main(String[] args)
        {
            
    Example1 ex1 = new Example1();
            
    String data ext.getData1();
            
    System.out.println(data1);
        }


    In this example, when we run the main method of the Example2 class, the first thing it does is create an object of type Example1. We then use this object reference to obtain the value using an accessor method. Accessor methods are simply get/set methods that either set a value, or return a value. We assign the value from the accessor method to the local variable "data". Lastly, we print the value of data, which is the string from the Example1 class.


    Hope that helps!

  • #3
    Senior Coder jerry62704's Avatar
    Join Date
    Oct 2007
    Location
    Springfield, IL
    Posts
    1,100
    Thanks
    13
    Thanked 87 Times in 87 Posts
    Put them all in the same package and the appropriate classes will be available to you.
    .
    .
    ...and gladly would he learn and gladly teach

    Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls

  • #4
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    There isn't really a single and/or a simple answer to your question. In order words, there are a number of ways to share information between objects...and as such, it all depends on your design and business requirements.

    Instead of listing any possible solutions, I think you should describe your requirement better so that we're not left wondering what you meant etc.

    Not that I like paraphrasing the following saying, but since it fits the situation so well..I will just write it anyway.

    THERE IS MORE THAN ONE WAY TO SKIN A CAT


  •  

    Posting Permissions

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