I've created a class called website, and want to access it like a variable so I can update values to it, probably better explained below:
Website w = new Website();
w.URL="stackoverflow.com";
Here's the code for the class:
class Website {
public String URL;
public Website(){
URL = "";
}
}
I would also like to add a method such as this:
public long save() {
return db.save(URL);
}
This (the method) isn't working for me at the moment
asked Apr 19, 2011 at 20:12
Todd Davies
5,5328 gold badges49 silver badges74 bronze badges
-
1I changed the class name from Credential to Website.yogsma– yogsma2011年04月19日 20:16:10 +00:00Commented Apr 19, 2011 at 20:16
-
Thanks, but the problem still remains...Todd Davies– Todd Davies2011年04月19日 20:17:00 +00:00Commented Apr 19, 2011 at 20:17
-
What is the error you are getting?yogsma– yogsma2011年04月19日 20:17:31 +00:00Commented Apr 19, 2011 at 20:17
-
A null pointer exceptionTodd Davies– Todd Davies2011年04月19日 20:17:54 +00:00Commented Apr 19, 2011 at 20:17
-
Other classes I've seen seem to use static, but this makes me unable to change the URL.Todd Davies– Todd Davies2011年04月19日 20:19:38 +00:00Commented Apr 19, 2011 at 20:19
1 Answer 1
I would do it more OO way, hiding this URL variable from outside and letting change it's value from getter and setter methods. You can try this, maybe this will help.
In Website class
public class Website {
private String URL;
public Website(){
this.URL = "";
}
public void setUrl(String url) {
this.URL = url;
}
public String getUrl() {
return this.URL;
}
public long save() {
return db.save(this.URL);
}
}
And then call it
Website w = new Website();
w.setUrl("http://www.stackoverflow.com");
long someLongValue = w.save();
answered Apr 19, 2011 at 20:29
evilone
22.8k8 gold badges85 silver badges112 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Todd Davies
This was the answer I used in the end, but thankyou to all who tried to answer!
lang-java