[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 934
  • Last Modified:

non-static variable this cannot be referenced from a static context

I'm just getting back into java and forgot what on earth this means
 non-static variable this cannot be referenced from a static context
import java.io.*;
import java.net.*;
import java.util.*;
public final class WebServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		int port = 6789;
		Socket sock = null;
		ServerSocket serverSocket = new ServerSocket(port);
		try 
		{
			sock = serverSocket.accept();
		
		} catch (IOException e) {
			print("Could not listen on port" + port);
			
		}
		while (true)
		{
			
			HttpRequest request = new HttpRequest(sock);
			Thread thread = new Thread(request);
			thread.start();
		}

	}
	
	final class HttpRequest implements Runnable
	{
		final static String CRLF = "\r\n";
		Socket socket;
		
		public HttpRequest(Socket socket) throws Exception
		{
			this.socket = socket;
		}
		
		public void run()
		{
			try {
				processRequest();
			} catch (Exception e) {
				System.out.println(e);
			}
			
		}
		
		private void processRequest() throws Exception
		{
			
			InputStream is = socket.getInputStream();
			DataOutputStream os = new DataOutputStream(socket.getOutputStream());
			
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String requestLine = br.readLine();
			
			print(requestLine);
			String headerLine = null;
			while ((headerLine = br.readLine()).length() !=0) {print(headerline);}
			os.close();
			br.close();
			socket.close();
			
			
		}
		
	}
	
	static void print(String string)
	{
		System.out.println(string);
	}

}

Open in new window

0
GPicasso
Asked:
GPicasso
  • 4
1 Solution
 
for_yanCommented:
You cannot reference instance bout not-static varibales of the class in the static method

0
 
for_yanCommented:
This is because HttpRequest is the inner class - it is like inastance varibale - you cannot referenc it in styatic main
0
 
for_yanCommented:
This does not give suhc error - I made HttpRequest norall class - not inner
It almmost compiles all, but hederline is not defined - but it is another story

import java.io.*;
import java.net.*;
import java.util.*;
public final class WebServer {


	public static void main(String[] args) throws Exception{
		int port = 6789;
		Socket sock = null;
		ServerSocket serverSocket = new ServerSocket(port);
		try
		{
			sock = serverSocket.accept();

		} catch (IOException e) {
			HttpRequest.print("Could not listen on port" + port);

		}
		while (true)
		{

			HttpRequest request = new HttpRequest(sock);
			Thread thread = new Thread(request);
			thread.start();
		}

	}

	

	

}

final class HttpRequest implements Runnable
	{
		final static String CRLF = "\r\n";
		Socket socket;

		public HttpRequest(Socket socket) throws Exception
		{
			this.socket = socket;
		}

		public void run()
		{
			try {
				processRequest();
			} catch (Exception e) {
				System.out.println(e);
			}

		}
        static void print(String string)
	{
		System.out.println(string);
	}

		private void processRequest() throws Exception
		{

			InputStream is = socket.getInputStream();
			DataOutputStream os = new DataOutputStream(socket.getOutputStream());

			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String requestLine = br.readLine();

			print(requestLine);
			String headerLine = null;
			while ((headerLine = br.readLine()).length() !=0) {print(headerline);}
			os.close();
			br.close();
			socket.close();


		}

	}

Open in new window

0
 
for_yanCommented:
OK, there was a misprint with case in hederline - this one compiles 100%:

import java.io.*;
import java.net.*;
import java.util.*;
public final class WebServer {


	public static void main(String[] args) throws Exception{
		int port = 6789;
		Socket sock = null;
		ServerSocket serverSocket = new ServerSocket(port);
		try
		{
			sock = serverSocket.accept();

		} catch (IOException e) {
			HttpRequest.print("Could not listen on port" + port);

		}
		while (true)
		{

			HttpRequest request = new HttpRequest(sock);
			Thread thread = new Thread(request);
			thread.start();
		}

	}





}

final class HttpRequest implements Runnable
	{
		final static String CRLF = "\r\n";
		Socket socket;

		public HttpRequest(Socket socket) throws Exception
		{
			this.socket = socket;
		}

		public void run()
		{
			try {
				processRequest();
			} catch (Exception e) {
				System.out.println(e);
			}

		}
        static void print(String string)
	{
		System.out.println(string);
	}

		private void processRequest() throws Exception
		{

			InputStream is = socket.getInputStream();
			DataOutputStream os = new DataOutputStream(socket.getOutputStream());

			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String requestLine = br.readLine();

			print(requestLine);
			String headerLine = null;
			while ((headerLine = br.readLine()).length() !=0) {print(headerLine);}
			os.close();
			br.close();
			socket.close();


		}

	}

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now