0
Why didn't need i to use a math class with out creating object of it?
5 ответов
+ 2
because they are static
+ 2
statc methods of a class can be accesed without creating object while non static methods you need to create a object to acces that specific method
+ 1
A static variable belongs to the class. PI is a static variable of the Math class. To access a static variable, you access it with the class. To show the value of pi (in Java), you would code the class name, a dot, and PI (like so: Math.PI). Since PI is a mathematical constant, it fits perfectly as belonging to the Math class.
An instance variable, on the other hand, belongs to each object of a particular class. For example, you may have a program with a Student class and use it to simulate 10 students. Each student (or each instance of a student) will have their own unique name. For example, student1.name may equal "Trevor" and student2.name may equal "Kyla"
Another way you might have a static variable might be to have a variable to track the number of students in the program. In this case, it might be called count. You would access it like so: Student.count. In my above example, Student.count would equal 10. This would only change if we added or subtracted a student.
The following article might help: http://www.javatpoint.com/static-keyword-in-java
0
Thanks but that moment i don't know the difference between static variable and nonstatic variables
- 1
Thanks a lot ☺