File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes variables one class accessed through another class??????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "variables one class accessed through another class???????" Watch "variables one class accessed through another class???????" New topic
Author

variables one class accessed through another class???????

ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello,


i am new to java programming, i need help that how to use the values(variables) of one class in another class. not with the constructors or by passing the parameters to method. actually i get the location values in gps class and i need those values in gprs class so that i can send them to some url through gprs class. it means that i need location values which we got in gps class in gprs class..

please help out:-)

thank u:-)
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
One way of doing it is by making the gprs class extend the gps class. But the point is do you really want it that way? Is gprs IS-A gps?
Also you cannot access private variables of the base class in the child class.

If you see no point in using inheritance, use getter setter methods for the variables.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Sample getter code in case you are new it..

ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
hello john jai,


Actually i have written the code for it please can you have a look on it and tell me what the problems or errors i may face. i will show only the imp part of the project.there are four classes GPS, Main , locatioonlistenerexample and GPRS. At present my project is like i need to retrieve the GPS location and send that values through GPRS to some URL. Now only we using gprs to send the GPS location but later many other applications come into picture which uses the gprs to send data. at present i am working on GPS using GPRS so in future this following code should not lead to an error so suggest if there any errors that may occur in future when many applications uses GPRS.




Class GPRS {

private static GPRS instance = null;

private GPRS{
any initalization
}
public static GPRS getInstance(){
synchronized(GPRS.class){
if(instance==null){
instance = new GPRS();
}
}
return instance;
}

public processing(){
synchronized(instance){
here it sends lat and long value to URL
}
}

}



public class LocationListenerExample implements LocationListener{


public double latitude, longitude;
//Gprs runnable = new Gprs(); no need


/after every 60secs this locationupdate is automatically called and updated the location value. its as per the GPS API
public void location Updated(LocationProvider locationprovider,
Location location){
.......
......
......
/*if (latitude!=0 && longitude!=0) {
synchronized(runnable) {

runnable.latitude = latitude;
runnable.longitude = longitude;
Thread ref = new Thread(runnable);
ref.start(); */

//instead of above
if (latitude!=0 && longitude!=0) {

GPRS.getInstance().processing();//use this line where ever you want to use GPRS class

}



}}}

}
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7494
    
  18

ashwini kalmath wrote:Actually i have written the code for it please can you have a look on it and tell me what the problems or errors i may face.

In order to help you, we need a piece of code that actually compiles (please see the ShowSomeEffort page). If you're having problems getting it to compile, then tell us the error message and we'll try to help there too. It will also help us (and you too I suspect) if the code doesn't have large sections that are commented out.

Also: please UseCodeTags.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
i need help that how to use the values(variables) of one class in another class. not with the constructors or by passing the parameters to method


make the latitude and longitude Static then use as - GPS.latitude and GPS.longitude
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7494
    
  18

Nomaan Butt wrote:make the latitude and longitude Static then use as - GPS.latitude and GPS.longitude

Hmmm. Not sure I agree with that. OP could end up with a very brittle structure as a result. I think we really need more information about what s/he wants to do.

@ashwini: I notice you said that you're new to Java, yet you're trying to write a reactive GPS class. This is no criticism, but I think you may be trying to take on too much too soon. Unless this is a requirement for a course, you might be better off starting with something a little less ambitious.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variables one class accessed through another class???????
 
Similar Threads
External Bluetooth GPS programming with J2ME
Using GPRS
sending sms using JavaScript
Synchronization
can we detect location if GPS is not accesible