0

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
9
  • 1
    I changed the class name from Credential to Website. Commented Apr 19, 2011 at 20:16
  • Thanks, but the problem still remains... Commented Apr 19, 2011 at 20:17
  • What is the error you are getting? Commented Apr 19, 2011 at 20:17
  • A null pointer exception Commented Apr 19, 2011 at 20:17
  • Other classes I've seen seem to use static, but this makes me unable to change the URL. Commented Apr 19, 2011 at 20:19

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

This was the answer I used in the end, but thankyou to all who tried to answer!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.