-2

How to call a method with out object reference.

I tried this calling this by

Byte().add();

I am getting an error

Raptor
  • 53,206
  • 45
  • 230
  • 366
my_551
  • 7
  • 2

2 Answers2

1

You cannot call an instance method without an object reference. The instance method belongs, by definition, to an instance of the object's class.

If the method were static, then you could call it using the name of the class. Static methods belong to the class definition itself, so no instance is required.

Also, your syntax is off, it's not

Byte().add();

It's

Byte.add(); //static method

or

new Byte().add() //instance method or static method, either can be called this way.
Kon
  • 10,702
  • 6
  • 41
  • 58
  • @user3260955 No problem. You can mark this question closed and answer as accepted by clicking the check mark under my score above, unless you have further questions. – Kon Mar 04 '14 at 05:07
1

You cant call an instance method without an Object reference. But if the method is static you can call it with class.

If you want to call a method without making an instance make that method static. Please learn difference between static and instance method

Community
  • 1
  • 1