简体   繁体   English

java中在方法内创建类实例的作用域

[英]The Scope of Creating an instance of a class inside a method in java

Can anyone explain to me the scope of creating an object from a class inside a method in java, is it a bad way and wasting the resources or it's normal?任何人都可以向我解释从 java 中的方法内的类创建对象的范围,这是一种浪费资源的坏方法还是正常的? The program is running but I am not sure about this step:程序正在运行,但我不确定这一步:

/**
 * Sending Packets Method
 * @param message-the message we want to send to the client side
 * @param IP-in InetAddress format
 * @param Port-in integer format
 * @return Null
 */
public static void send(String message,InetAddress IP,int Port){
    byte [] buffer=message.getBytes();
    DatagramPacket datagrampacket=new DatagramPacket(buffer,buffer.length,IP,Port); 
    datagrampacket.setPort(20002);
    try {
        DatagramSocket datagramSocket=new DatagramSocket();
        datagramSocket.send(datagrampacket);
        datagramSocket.setReuseAddress(true);
        datagramSocket.close();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

In this case, I create a new object of DatagramSocket every single time I call the function and wasting the resources or the object disappear when the function finishes?在这种情况下,我每次调用函数时都会创建一个新的 DatagramSocket 对象并浪费资源或函数完成后对象消失?

Thanks谢谢

It is not wrong but it is sub-optimal, a DatagramSocket doesn't qualify as a lightweight class for the purposes of construction on demand.这并没有错,但它是次优的,出于按需构建的目的,DatagramSocket 不符合轻量级类的要求。 This question would probably receive better answers on SE Code Review .这个问题可能会在SE Code Review上得到更好的答案。

  • Make the datagramSocket a property of the class.使datagramSocket成为类的属性。
  • Remove the static from the method.从方法中删除静态。
  • Instantiate the socket in either constructor of this class, or the calling class and pass to the constructor.在此类的构造函数或调用类中实例化套接字并传递给构造函数。
  • Your parameter variable 'Port' should be 'port'.您的参数变量“端口”应该是“端口”。
  • Your code ignores the Port parameter and hard codes a socket to 20002.您的代码会忽略 Port 参数并将套接字硬编码为 20002。
  • Learn the distinction between variable lifetime and scope .了解变量生命周期和作用域之间区别

The scope will be the declaration of the object.范围将是对象的声明。 To create a object in a function, it depends upon yourrequirement, if you want to reuse the object, then you can declare the object at the class level and make the class as singleton (like using the Database repository) and initilize it in the constructor or some init method.要在函数中创建对象,这取决于您的需求,如果您想重用该对象,则可以在类级别声明该对象并将该类设为单例(如使用数据库存储库)并在构造函数中初始化它或一些初始化方法。 And if you require every method call should have new object, then you should create in method level.如果您要求每个方法调用都应该有新对象,那么您应该在方法级别创建。

since you are not returning the object to any instance variable, when the methd ends there are no variable pointing to the object so it can be garbage collected.由于您没有将对象返回给任何实例变量,因此当方法结束时,没有指向该对象的变量,因此可以对其进行垃圾回收。 you can not know when the gb will run or if it will.您不知道 gb 何时会运行或是否会运行。 this is just to say you can not assume the object will disappear as soon as the method ends这只是说你不能假设对象会在方法结束后立即消失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM