Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2008, 05:25 AM   PM User | #1
Lococard
New to the CF scene

 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Lococard is an unknown quantity at this point
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
Lococard is offline   Reply With Quote
Old 08-22-2008, 06:27 PM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
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!
brad211987 is offline   Reply With Quote
Old 08-22-2008, 09:29 PM   PM User | #3
jerry62704
Senior Coder

 
jerry62704's Avatar
 
Join Date: Oct 2007
Location: Springfield, IL
Posts: 1,046
Thanks: 9
Thanked 81 Times in 81 Posts
jerry62704 is on a distinguished road
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
jerry62704 is offline   Reply With Quote
Old 08-28-2008, 08:31 PM   PM User | #4
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
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
ess is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:04 PM.


Advertisement
Log in to turn off these ads.