Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on What are the disadvantages of using static methods in Java?

Parent

What are the disadvantages of using static methods in Java?

+2
−5

I was using static method few moments ago. But I noticed that I was returning a variable using that static method. According to the article,

Static is a keyword that identifies the class-related thing. It means the given Method or variable is not instance-related but Class related. It can be accessed without creating the instance of a Class.

I think I can access the static method without caring of class, isn't it? Is there something special of static method? static method can helps to decrease amount of source code. So Is it better to use static method? Or is there any limit of it?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

I have changed the title to what I think is better summarising the content. Feel free to change somet... (2 comments)
Post
+0
−2

If it were always better to use static, the language would have been designed to always assume static, or at least default to static. That the language defaults to making methods not static indicates that the designers of the programming language envisioned static methods to be rare.

Java is intended to be an object oriented programming language, where a program is a set of objects that "talk" to each other using methods. Objects are created from classes, and classes can be related through inheritance. Specifically, a class can be declared to extend another class, which allows it to inherit or override methods from the parent class. For instance:

class Animal {
   void move() {
       System.out.prinln("The animal moves");
   }
}

class Dog extends Animal {
   @Override
   void move() {
       System.out.println("The dog walks");
   }
}

class Snake extends Animal {
   @Override
   void move() {
       System.out.println("The snake slithers");
   }
}

Which you can then use as follows:

Animal animal = new Snake();
animal.move(); // "The snake slithers"

So even though the variable contains an Animal, that Animal knows whether it is a Dog or a Snake, and responds accordingly when it is asked to move.

This ability to refine the behavior of a class in a subclass is very useful. However, since this depends on the type of the object, it is only available for methods of objects, not static methods.

By using static, you deny yourself this useful tool. This may seem a small price to pay when your programs are tiny and consist only of a few isolated methods. However, once your programs get bigger, and particularly once they start incorporating code written by other people, the ability to refine code they have written will be invaluable.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Feel free to explain downvotes (3 comments)
Feel free to explain downvotes
meriton‭ wrote over 2 years ago

Did I say something wrong? Did I not say something you think I should? I tried to say the same as Alexei, but in a way more understandable to beginners, but he got +3, I got -2. Am I to conclude I should never use simple language here?

Anonymous‭ wrote over 2 years ago · edited over 2 years ago

Specifically, a class can be declared to extend another class, which allows it to inherit or override methods from the parent class.

Class can forever extend any class. But method can't; static method either. Your answer was about class rather than method. That's why I had downvoted... Even in your example you didn't add anything about method.

I don't have idea of another downvoter. If you didn't understand my question then I am responsible for that Sorry..!

meriton‭ wrote over 2 years ago

Erm, what? My answer says that static methods can not be overridden in a subclass. Which is their main disadvantage. I don't get how you came conclude that my "example didn't add anything about method" when that example declares 3 methods, and proceeds to call one of them in a way that wouldn't be possible if the method were static. (I felt I had to mention classes and subclassing because overriding is only possible in subclasses)