+ 4

Help about static keyword in java

Why we write static before the function ? what it mean ? And why it gives error if we not write static ?

2nd Jan 2018, 2:57 PM
Ronak Pareek
Ronak Pareek - avatar
6 odpowiedzi
+ 4
Many say java is not a "pure" object oriented language because java supports primitive data types (boolean, int float etc). For a language to be pure object oriented there should be only objects and classes. However there is no actual definition of "pure" or 100% object oriented language. But that should not effect you in any way. Having primitive data types is a good thing.
4th Jan 2018, 2:29 AM
Peerzada Burhan
Peerzada Burhan - avatar
+ 8
Both variables and methods can be static. By declaring them static we do not need to create objects of the class but we can directly use them by the calling the class eg public class MyClass{ public static String name = "Ahmed"; public static void myMethod(){ System.out.println("Hello"); } } public class B{ public static void main(String[] args){ MyClass.myMethod(); System.out.println(MyClass.name); } } Here i used static variable and method of one class in another class without creating its object. You are getting error because static methods cannot access non-static methods. Since main() method is always static so if you want to access your method from it you have to declare it static.
2nd Jan 2018, 3:11 PM
Peerzada Burhan
Peerzada Burhan - avatar
+ 6
static mean that you don't need object to call some method, method belong to it's class
2nd Jan 2018, 3:04 PM
Vukan
Vukan - avatar
+ 5
No java is an object oriented language. Objects are its integral part.
3rd Jan 2018, 4:03 AM
Peerzada Burhan
Peerzada Burhan - avatar
+ 1
is it true that there is nothing like object in java ?
3rd Jan 2018, 3:21 AM
Ronak Pareek
Ronak Pareek - avatar
+ 1
and why we say java is nearly 100% object oriented language why not total 100%
3rd Jan 2018, 4:36 AM
Ronak Pareek
Ronak Pareek - avatar