Home >Java >javaTutorial >Common mistakes and precautions when using Java internal classes

Common mistakes and precautions when using Java internal classes

WBOY
WBOYforward
2023-04-21 09:16:15964browse

1. Handling of memory leaks

has nothing to do with class member methods and member variable methods. It is best to define them as static.

public class Outer{
 
    public static List<String> getList(String item) {
 
        return new ArrayList<String>() {
            {
                add(item);
            }
        };
    }
}

2. Suitable for implementation classes that only implement one interface

Try not to use Thread directly. Here, if using only Java8, it is recommended to use lambda instead for such an application.

       new Thread(new Runnable() {
           @Override
           public void run() {
               System.out.println("test");
           }
       }
 
       ).start();
    }

The above is the detailed content of Common mistakes and precautions when using Java internal classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete