0

The string title cannot be static because of the intent.. The string URL has to be static for the usage of it.. This means i get the error when i try to add a non-static string to a static string. how can i make it work?

Error: Cannot make a static reference to the non-static field title

Intent i = getIntent();
String title = i.getStringExtra("title");


static final String URL = "http://csddata.site11.com/dynamic.php?cat=" + title;

Thanks.

Tom Doe
  • 331
  • 6
  • 23
  • 3
    Why do you want to make `URL` static? If you do not make it static it should work. – Uwe Plonus Jun 07 '13 at 10:57
  • This question was already [answered elsewhere](http://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-method?rq=1) – zEro Jun 07 '13 at 11:11

2 Answers2

2

If you really do need to keep your string static and final you could do

static final String URL = "http://csddata.site11.com/dynamic.php?cat=%s";

Intent i = getIntent();
String title = i.getStringExtra("title");

String finalUrl = String.format(URL,title);
Akash
  • 631
  • 1
  • 8
  • 16
1

This wont work because you will be getting value of title when the present activity is launched.

The String URL is static & final. Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables. Declaring the field as 'final' will ensure that the field is a constant and cannot change.

Intent i = getIntent();
String title = i.getStringExtra("title");


String URL = "http://csddata.site11.com/dynamic.php?cat=" + title;

Your code must fine now!!

onkar
  • 4,427
  • 10
  • 52
  • 89