non static varible from static context error
in this class i have a constructor and a main() function for testing the constructor
theres a couple things stopping me from testing it in the command prompt tho
the post data is a string with an ampersand in it and when i try to pass it to the main function in the command prompt it thinks that the ampersand means something, i dont know how to "escape" it
also, i would like to know how to pass a value from the constructor to main() so i can print string test, i tried putting it outside both meathods into the class but it gave me non static context error
Code :
//takes am address, and post data, and returns the html as a string
import java.net.*;
import java.io.*;
import java.lang.*;
public class UrlToString
{
public String test = "";
public static void main (String[] args) throws Exception
{
//enter url and post data in cmd prompt to test
new UrlToString (test,args[0],args[1]);
System.out.println(test);
}
//paramaters of returned string, address to get it from, and post data in th for of variable1=value1&variable2=value2...
UrlToString (String returnString, String inAddr, String rawData) throws Exception
{
returnString = "";
//connection variables
String agent = "Mozilla/4.0";
String type = "application/x-www-form-urlencoded";
HttpURLConnection connection = null;
String encodedData = URLEncoder.encode( rawData, "UTF-8");
//connect
try
{
URL searchUrl = new URL(inAddr);
connection = (HttpURLConnection)searchUrl.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty( "User-Agent", agent );
connection.setRequestProperty( "Content-Type", type );
connection.setRequestProperty( "Content-Length", Integer.toString(encodedData.length()) );
OutputStream os = connection.getOutputStream();
os.write( encodedData.getBytes() );
os.flush();
os.close();
//check if theres an http error
int rc = connection.getResponseCode();
if(rc==200)
{
//no http response code error
//read the result from the server
InputStreamReader in = new InputStreamReader(connection.getInputStream());
BufferedReader br = new BufferedReader(in);
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
returnString = returnString.concat(strLine);
System.out.println(strLine);
}
return;
}
else
{
System.out.println("http response code error: "+rc+"\n");
return;
}
}
catch( IOException e )
{
System.out.println("search URL connect failed: " + e.getMessage());
e.printStackTrace();
}
}
}
Re: non static varible from static context error
It means that you're calling a non-static variable in a static method.
For starters, try making the variable test be static.
Re: non static varible from static context error
A non-static variable only exists as part of an instance of a class. When you create a class object, all its non-static variables are created with it. Each class object has its own version of each non-static variable.
A static variable is shared by all instances of a class. There is only one version of that variable.
Why is test a class variable instead of a local variable in the main() method?
Re: non static varible from static context error
Quote:
Originally Posted by
Norm
A non-static variable only exists as part of an instance of a class. When you create a class object, all its non-static variables are created with it. Each class object has its own version of each non-static variable.
A static variable is shared by all instances of a class. There is only one version of that variable.
Why is test a class variable instead of a local variable in the main() method?
It would also work in the main method too.
It would be better there, but I was assuming that the OP had a reason for not having the variable in the main method.
Re: non static varible from static context error
Quote:
Originally Posted by
Norm
A non-static variable only exists as part of an instance of a class. When you create a class object, all its non-static variables are created with it. Each class object has its own version of each non-static variable.
A static variable is shared by all instances of a class. There is only one version of that variable.
Why is test a class variable instead of a local variable in the main() method?
because if i put it in main() it would pass to the constructor as local value not reference and would not change, i want test to be changed by the constructor and then go back to main after the constructor call, and then print it
Re: non static varible from static context error
If you want the main() method to be able to reference a class variable, you need to save a reference to the created instance of the class and use that to access its variables. The posted code does NOT save a reference to the instance when it uses new to create the instance. Save the reference in a variable and use that variable to access the test variable.
Re: non static varible from static context error
omfg ur right holy fuck im dumb
ok but still, its confusing, how i get rid of this error?
if i make the variable static i cant change it
Re: non static varible from static context error
Quote:
how i get rid of this error?
See post #6
Re: non static varible from static context error
wait nm i got it im dumb i nedded to pass it an empty strin, chich sems kind of wierd
Re: non static varible from static context error
Please post the full text of the error message.
Re: non static varible from static context error
i got it
sorry for the dumb questions lol i just have been doing alot of procedurally oriented programming lately not much object oreinted
i still dont know how to pass an ampersand as part of a string to a class in the command line tho, i need to escape it or something
Re: non static varible from static context error
Sorry, I don't know much about how you are trying to make the OS do something.
Re: non static varible from static context error
That ampersand thing, I'm not sure exactly what you mean, but you don't mean a reference do you? That's C++, not Java. C is procedure oriented. C++ is object oriented.
However, perhaps C also has pass by reference params, and I think it does actually. I know it allows you to pass pointers. (I know it does for C-Strings.)
Java isn't that great for messing with the operating system at the lower levels. C++ would be better, as it has C and allows procedure oriented as well as OO.
If you're talking about C++ passing &, then I don't think it would be any different, at least param wise, as passing a regular param.