Static and Fixed methods:
So guys in this post we are going to discuss what is static and fixed variable in Java, both static and fixed method we will see in detail.so let's get started.Static Method in java:
Static methods are methods that do not operate on objects. A static method acts related to a class declare a static method by using the static keyword as a modifier. To declare a method as static is as follows:
Static returntype methodname(parameter)
{
//body of the method
}
We can call a static method from the outside of a class as follow:
classname.staticmethodname(parameters);
A method declared as static has several restrictions:
- They can only call the other static method.
- They must only access static data.
- They cannot refer to this in any other way.
Program:
class staticdemo
{
static void print()
{
System.out.println("java programming");
}
public static void main(String args[])
{
print();
}
}
Fixed method:
A fixed method is one which cannot be overridden by a subclass. A fixed method can be declared by prefixing the word-final before a method. We can use the keyword final to donate a constant.Program:
class finaldemo
{
public static void main(String arg[])
{
double paperwidth=8.5;
double paperheight=11;
System.out.println("paper size in centimeter:" +paperwidth*CM_PER_INCH+"by"+paperheight*CM_PER_INCH);
public static final double CM_PER_INCH=2.45;
}
}
Guyz I hope you guys understand what is a static and fixed method in Java and if you have any queries you can comment me and help me to improve my blog because I am a new blogger and thank you for visiting my blog.
No comments:
Post a Comment