8 Replies - 2042 Views - Last Post: 28 June 2009 - 09:39 PM Rate Topic: -----

#1 Devinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 09

Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 03:16 PM

uhm I get the error non-static method getVersion() cannot be referenced from a static context in this line:
short version = (short) ChannelServer.getVersion();

heres full:

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		log.info("IoSession with {} opened", session.getRemoteAddress());

		if (channel > -1) {
			if (ChannelServer.getInstance(channel).isShutdown()) {
				session.close();
				return;
			}
		}

		byte key[] = { 0x13, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, (byte) 0xB4, 0x00, 0x00,
			0x00, 0x1B, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00 };
		byte ivRecv[] = { 70, 114, 122, 82 };
		byte ivSend[] = { 82, 48, 120, 115 };

		ivRecv[3] = (byte) (Math.random() * 255);
		ivSend[3] = (byte) (Math.random() * 255);
		short version = (short) ChannelServer.getVersion();
		MapleAESOFB sendCypher = new MapleAESOFB(key, ivSend, (short) (0xFFFF - version));
		MapleAESOFB recvCypher = new MapleAESOFB(key, ivRecv, version);

		MapleClient client = new MapleClient(sendCypher, recvCypher, session);
		client.setChannel(channel);

		session.write(MaplePacketCreator.getHello(version, ivSend, ivRecv, false));
		session.setAttribute(MapleClient.CLIENT_KEY, client);
		session.setIdleTime(IdleStatus.READER_IDLE, 30);
		session.setIdleTime(IdleStatus.WRITER_IDLE, 30);
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Non-static method getVersion() cannot be referenced from a static cont

#2 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 03:45 PM

The compiler gave you a descriptive error. What is it exactly that you don't understand about it?
Was This Post Helpful? 0
  • +
  • -

#3 Devinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 09

Re: Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 03:47 PM

View PostDantheman, on 28 Jun, 2009 - 02:45 PM, said:

The compiler gave you a descriptive error. What is it exactly that you don't understand about it?

I suck at Java?
Was This Post Helpful? 0
  • +
  • -

#4 Devinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 09

Re: Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 03:58 PM

Can anyone help me? I suck at Java
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 04:04 PM

If you calling a method directly from a static context (like main), then you need to make that method static or use it in a non static context.
Was This Post Helpful? 0
  • +
  • -

#6 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 06:48 PM

Static and non-static stuff can be confusing, but once you understand it, then it makes perfect sense. Static basically means "once per class", and non-static means "once per instance".
In this situation, ChannelServer is a class, not an instance of that class. When you call a method through a class, the method must be static. Since "getVersion()" is not a static method, it must be called through an instance of the ChannelServer class, not through the class name.

Example, to call a static method:
ClassName.method();



but to call a non-static method:
ClassName instance = new ClassName();
instance.method();



Does this make sense?

Hope this helps!
-Michael
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 08:37 PM

To avoid that type of problem, the easiest solution is

class MyClass {

	// MyClass constructor
	MyClass() {
	  .... do all your stuff here
	}

	// the only one static method in yoiur .java file
	public static void main(String[] args) {
	   new MyClass();
	   ... that's it
	}
}


Was This Post Helpful? 1
  • +
  • -

#8 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 09:33 PM

Right, but isn't the problem with the way that he is calling the .getVersion() method? I could be wrong, but I don't see how the main method has anything to do with this? He just needs to create a ChannelServer object first and make the call using that instead of the class name, right?

but like I said...I could definitely be wrong, seeing as how pbl and kya are the resident experts around here:-)

-Michael
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Non-static method getVersion() cannot be referenced from a static cont

Posted 28 June 2009 - 09:39 PM

View PostMikeyp926, on 28 Jun, 2009 - 08:33 PM, said:

Right, but isn't the problem with the way that he is calling the .getVersion() method? I could be wrong, but I don't see how the main method has anything to do with this? He just needs to create a ChannelServer object first and make the call using that instead of the class name, right?

but like I said...I could definitely be wrong, seeing as how pbl and kya are the resident experts around here:-)

-Michael

ChannelServer.getVersion();

is trying a static method in ChannelVersion class
does the class ChannelServer offers a static method getVersion()

he didn't post all his code but I guess that getVersion() should be called on an instance of ChannelServer

just a guess :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1