-4

I am relatively an amateur in java and have been calling static variables and methods using both classes and objects all the time. When they are static they can be used by both classes and objects.

Why didn't the programmers just keep the default static for both class variables and methods?

Omar Boshra
  • 447
  • 7
  • 21
  • Possible duplicate question: [why not everything is static function in java any differences in following two](http://stackoverflow.com/questions/33902452/why-not-everything-is-static-function-in-java-any-differences-in-following-two?noredirect=1&lq=1) – Jeroen Vandevelde Mar 05 '17 at 00:46
  • You don't think those very smart people that created Java had a reason to support both ? http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class – Martin Spamer Mar 05 '17 at 00:47
  • Possible duplicate of [Java: when to use static methods](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods) – buræquete Mar 05 '17 at 00:47
  • Global variables and state are bad. Singletons are bad. Object orientation requires there be objects. Why is there more than one person or book or tree or fish in the world, instead of just one of each? – Lew Bloch Mar 05 '17 at 03:02

1 Answers1

0

For a static method, we don’t specify an object at all, we simply write the class name and then the method name. This means that inside the static method, there’s nothing that we can call this, so any reference to it (or any reference to any non-static field) causes the compiler to throw an error because there’s no object it can use.

  • Your right "this" variable cant be done in a static method .I am just wondering since even "this" is a non static variable .If all variables were made static so where the methods it would be much better since not only classes can call them but also objects . – Omar Boshra Mar 05 '17 at 01:02
  • I´m gonna answer you with this: http://softwareengineering.stackexchange.com/questions/98083/cant-i-just-use-all-static-methods read the top answer, it should give you a wider concept of the problems with static. :) –  Mar 05 '17 at 01:10
  • This is the BEST complete answer I found its perfect http://www.codejava.net/java-core/the-java-language/differences-between-static-and-non-static-stuffs-in-java – Omar Boshra Mar 05 '17 at 01:28