最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Java中如何类的初始化

Java crifan 1227浏览 0评论
折腾:
【未解决】Java给OkHttp加cookie出错:Cannot resolve symbol setCookiePolicy和Unknown class CookiePolicy.ACCEPT_ALL
期间,需要优化代码,把本来在全局部分的代码,放在类的初始化。
所以需要去搞清楚Java中类的初始化。
java class init
java class initialization
Java 101: Class and object initialization in Java | JavaWorld
Object initialization in Java | JavaWorld
Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
A Guide to Java Initialization | Baeldung
java 类的初始化
Java 类初始化(详解) – acherie的博客 – CSDN博客
【深入Java虚拟机】之三:类初始化 – 兰亭风雨的专栏 – CSDN博客
Java类初始化顺序 – code-craft – SegmentFault 思否
java 类 构造函数
java class constructor
Java 构造函数的详解 – 重心开始,重新开始 – CSDN博客
Java中的构造方法总结 – Android Explorer – CSDN博客
java构造函数(构造方法) – 简书
Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
还是这个帖子介绍的简洁易懂:
Constructors in Java – A complete study!!
  • “1. Every class has a constructor whether it’s a normal class or a abstract class.
  • 2. Constructors are not methods and they don’t have any return type.
  • 3. Constructor name should match with class name .
  • 4. Constructor can use any access specifier, they can be declared as private also. Private constructors are possible in java but there scope is within the class only.
  • 5. Like constructors method can also have name same as class name, but still they have return type, though which we can identify them that they are methods not constructors.
  • 6. If you don’t implement any constructor within the class, compiler will do it for.
  • 7. this() and super() should be the first statement in the constructor code. If you don’t mention them, compiler does it for you accordingly.
  • 8. Constructor overloading is possible but overriding is not possible. Which means we can have overloaded constructor in our class but we can’t override a constructor.
  • 9. Constructors can not be inherited.
  • 10. If Super class doesn’t have a no-arg(default) constructor then compiler would not insert a default constructor in child class as it does in normal scenario.
  • 11. Interfaces do not have constructors.
  • 12. Abstract class can have constructor and it gets invoked when a class, which implements interface, is instantiated. (i.e. object creation of concrete class).
  • 13. A constructor can also invoke another constructor of the same class – By using this(). If you want to invoke a parameterized constructor then do it like this: this(parameter list).”
【总结】
此处初始化可以写成:
public class EmulateBaiduLogin {
    private static CookieManager cookieManager;
    private static OkHttpClient httpClient;

    EmulateBaiduLogin(){
        cookieManager = new CookieManager();
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        httpClient = new OkHttpClient.Builder()
            .addInterceptor(new LoggingInterceptor())
            .addInterceptor(new ResponseInterceptor())
            .cookieJar(new JavaNetCookieJar(cookieManager))
            .build();
    }
}

转载请注明:在路上 » 【已解决】Java中如何类的初始化

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.198 seconds, using 22.10MB memory