sinnnad
sinnnad

Reputation: 9

Can I write a code only using static(class) methods?

So my question is based on my homework instruction.

The professor wouldn't answer instantly so here I am!

"Use static (class) methods to create and print 2-dimensional arrays of characters in different patterns." That is the objective of the assignment.

That does mean I have to write the code only using public class () iAmDumbSorry methods?

Or am I misunderstanding something?

Upvotes: 0

Views: 47

Answers (2)

GhostCat
GhostCat

Reputation: 140613

Sure, it is possible to write java code that solely relies on static fields/methods.

But the point is: static has various implications, as it simply kills your ability to override methods. In other words: static-only means: no polymorphism. Which makes using an OOP language almost pointless.

Sometimes an education plan starts with "static only", assuming that this is easier for students to follow. And then the non-static things are excluded, and explained later on.

So your code would look like this:

class Foo {
  static int someInt;
  static void bar() { ... }

and

public class Main {
  public static void main(... {
    Foo.bar();
    if (Foo.someInt == ) ..

Long story short: take this unclear assignment as motivation to learn about the differences between static and non-static, and see what effects it has on your ability to express your thoughts in code.

Upvotes: 1

Murat Karagöz
Murat Karagöz

Reputation: 37604

Yes, those classes are called utility classes. You can compare it to the java.util.Collections which consists only of static methods.

Upvotes: 0

Related Questions