www.webdeveloper.com
Results 1 to 2 of 2

Thread: Non-static method cannot be referenced from a static context

  1. #1
    Join Date
    Jun 2011
    Posts
    1

    Non-static method cannot be referenced from a static context

    Hi,

    Help will be appreciated ALOT.

    These are my codes:

    redTeam.java

    Code:
    package com.rs2hd.teams;
    
    import java.io.*;
    
    public class redTeam extends RandomAccessFile {
    
    	// constructor
    	Counter(String red) throws IOException {
    		super(new File(red), "rw");
    		writeInt(0);
    	}
    
    	public void addToCounter() {
    		addToCounter(1);
    	}
    	
    	public void addToCounter(int delta)  {
    		try {
    			int n = getValue() + delta;
    			seek(0);
    			writeInt(n);
    		}
    		catch(IOException e) {}
    	}
    
    	public int getValue() {
    		try {
    			seek(0);
    			return readInt();
    		}
    		catch (IOException e) {}
    		return -1;
    	}
    }
    greenTeam.java

    Code:
    package com.rs2hd.teams;
    
    import java.io.*;
    
    public class greenTeam extends RandomAccessFile {
    
    	// constructor
    	Counter(String green) throws IOException {
    		super(new File(green), "rw");
    		writeInt(0);
    	}
    
    	public void addToCounter() {
    		addToCounter(1);
    	}
    	
    	public void addToCounter(int delta)  {
    		try {
    			int n = getValue() + delta;
    			seek(0);
    			writeInt(n);
    		}
    		catch(IOException e) {}
    	}
    
    	public int getValue() {
    		try {
    			seek(0);
    			return readInt();
    		}
    		catch (IOException e) {}
    		return -1;
    	}
    }
    blueTeam.java

    Code:
    package com.rs2hd.teams;
    
    import java.io.*;
    
    public class blueTeam extends RandomAccessFile {
    
    	// constructor
    	Counter(String blue) throws IOException {
    		super(new File(blue), "rw");
    		writeInt(0);
    	}
    
    	public void addToCounter() {
    		addToCounter(1);
    	}
    	
    	public void addToCounter(int delta)  {
    		try {
    			int n = getValue() + delta;
    			seek(0);
    			writeInt(n);
    		}
    		catch(IOException e) {}
    	}
    
    	public int getValue() {
    		try {
    			seek(0);
    			return readInt();
    		}
    		catch (IOException e) {}
    		return -1;
    	}
    }
    These are the errors I get:
    Compiling...
    src\com\rs2hd\teams\redTeam.java:8: invalid method declaration; return type requ
    ired
    Counter(String red) throws IOException {
    ^
    src\com\rs2hd\packethandler\CommandsPacketHandler.java:711: non-static method ad
    dToCounter() cannot be referenced from a static context
    redTeam.addToCounter();
    ^
    src\com\rs2hd\teams\blueTeam.java:8: invalid method declaration; return type req
    uired
    Counter(String blue) throws IOException {
    ^
    src\com\rs2hd\packethandler\CommandsPacketHandler.java:714: non-static method ad
    dToCounter() cannot be referenced from a static context
    blueTeam.addToCounter();
    ^
    src\com\rs2hd\teams\greenTeam.java:8: invalid method declaration; return type re
    quired
    Counter(String green) throws IOException {
    ^
    src\com\rs2hd\packethandler\CommandsPacketHandler.java:717: non-static method ad
    dToCounter() cannot be referenced from a static context
    greenTeam.addToCounter();
    ^
    src\com\rs2hd\packethandler\CommandsPacketHandler.java:720: non-static method ge
    tValue() cannot be referenced from a static context
    int red = redTeam.getValue();
    ^
    src\com\rs2hd\packethandler\CommandsPacketHandler.java:721: non-static method ge
    tValue() cannot be referenced from a static context
    int green = greenTeam.getValue();
    ^
    src\com\rs2hd\packethandler\CommandsPacketHandler.java:722: non-static method ge
    tValue() cannot be referenced from a static context
    int blue = blueTeam.getValue();
    ^
    9 errors
    Druk op een toets om door te gaan. . .

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    1.add public (or some other based upon your design) identifier before the constructor name declaration, and if you are returning a value in the method signature you have to make sure that u use return zzzz; at the end of the method inside its body
    2:Again if you want to access a static method you should use as ClassName.StaticMethod instead.

    HTH
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles