Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: non static varible from static context error

  1. #1
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    //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();
    		}
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.
    Last edited by javapenguin; June 4th, 2012 at 03:00 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    Last edited by Norm; June 4th, 2012 at 03:15 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: non static varible from static context error

    Quote Originally Posted by Norm View Post
    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.

  5. #5
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: non static varible from static context error

    Quote Originally Posted by Norm View Post
    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: non static varible from static context error

    how i get rid of this error?
    See post #6
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by chopficaro; June 5th, 2012 at 03:36 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: non static varible from static context error

    Please post the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.
    Last edited by javapenguin; June 5th, 2012 at 07:12 PM.

Similar Threads

  1. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  2. static context
    By mydoom in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 25th, 2011, 03:37 PM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM