File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ashwini kalmath
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sample getter code in case you are new it..

 
ashwini kalmath
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9442
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nomaan Butt
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9442
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic