Java: why not declare static intead that passes references?

Suppose we want to make a game where you have to collect gems So we need a gem class, a gemspawner class and, of course, a mainactivity class

public class MainActivity {
    public static void main(String[] args) {

        List<Gem> gems = new ArrayList<Gem>();

        GemSpawner gs = new GemSpawner(gems);

        //...
    }
}

In this case, we use gems to create a list and pass it to gemowner through its constructor, so GS we can add gems to the list with the following command:

gems.add(new Gem(10,50,"red")); //should represent Xpos,Ypos,and color.

But it won't be better:

public class MainActivity {

    public static List<Gem> gems = new ArrayList<Gem>();

    public static void main(String[] args) {

        GemSpawner gs = new GemSpawner();

        //...
    }
}

Gemspaowner (GS) can now add gemstones using the following command:

MainActivity.gems.add(new Gem(10,"red"));

My friend only showed me and explained the above methods, but isn't the following more effective?

Solution

This is a more complicated problem than you may realize

Many people who started writing in Java began to completely static everything I did because you didn't have to pass references - it made your code "simpler"

However, when your code becomes more complex, you begin to encounter problems There are three main lines to these problems:

>Encapsulation > abstraction > test

Encapsulation

This is the idea that an object should not allow direct access to its members. It should be told to "do things", and then it will be done internally without exposing how it is done

The idea behind this is that you try to avoid coupling your classes too closely

This leads us to the next point

Abstraction

In Java, this is represented by abstract classes and interfaces

The idea is that your gemspawner just produces the definition of a gem How to do this internally is really not a person's business, but its own business

In Java, you can't really reconcile static methods with key OO inheritance concepts

Static methods are inherited, but they are masked rather than overwritten, so you cannot (easily) modify their behavior

This leads us into

test

As the program becomes more and more complex, this topic becomes more and more

How do you test the "Hello world" program? Well, you run it and see if it prints "Hello wrld" – in this case, there is an error

Once the program becomes more complex, you can't simply do this You need to separate your program and test the "unit" It is called unit testing

Here your static references are really starting to cause problems You cannot separate a program into discrete units because everything is bound together by a direct class reference And you cannot simulate the behavior of static methods because they are not easily overridden

So, to sum up Yes; It may be faster and easier to place static anywhere rather than passing references However, if you plan to write something as complex as a game, you should consider using java to realize its full potential

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>