简体   繁体   English

当我们使用类实例调用静态方法/函数时会发生什么?

[英]What happens when we call a static method/function using class instance?

Static variables and method belong to class rather than it's instances(Objects). 静态变量和方法属于类而不是它的实例(对象)。 generally they are accessed using className.staticMethod() or className.staticVariable but we can also access or call them using class instances eg classInstance.staticMethod() and classInstance.staticVariable . 通常使用className.staticMethod()className.staticVariable访问它们,但我们也可以使用类实例访问或调用它们,例如classInstance.staticMethod()classInstance.staticVariable

My 1st question is 我的第一个问题是

Why allow accessing static methods/functions through class instances in the 1st place? 为什么允许在第一个位置通过类实例访问静态方法/函数? Does it have any use case? 它有任何用例吗?

Next question is as follows.Consider the following class 下一个问题如下。考虑以下课程

public class Counter{
private static int count = 0;

public static synchronized int getCount()
{
  return count;
}

public synchronized setCount(int count)
{
   this.count = count;
}

}

In a multi threaded environment if these functions are called as below 在多线程环境中,如果调用这些函数如下所示

Counter myCounter = new Counter();
myCounter.setCount(10);
System.out.println(myCounter.getCount());

2nd question is 第二个问题是

Will both functions have separate lock or the same lock(considering they are both called by myCounter object)? 两个函数是否都有单独的锁或相同的锁(考虑到它们都被myCounter对象调用)? If thread processing static method still acquiring class level lock how does it internally figure out what lock it has to take? 如果线程处理静态方法仍然获取类级锁定它如何在内部找出它必须采取什么锁?

Question1 : An class instance always has information of a class, so it is valid to call a static method from instance as it will have information regarding class. 问题1 :类实例始终具有类的信息,因此从实例调用静态方法是有效的,因为它将具有关于类的信息。 Other way round is not possible as class will not have info regarding its instances, so you can't call instance level method using class name. 其他方式是不可能的,因为类不会有关于其实例的信息,因此您不能使用类名调用实例级方法。

If you try to call a static method via an instance you will get warning as you should avoid it, so you wont get a valid case but yes its logical to allow static call through instance. 如果你试图通过一个实例调用一个静态方法,你会得到警告,因为你应该避免它,所以你不会得到一个有效的情况,但是它是合乎逻辑的,允许通过实例进行静态调用。

Question2 : Coming to 2nd question, moment a thread enters a static block or method, jvm knows which lock to take ie class level. 问题2 :进入第二个问题,当一个线程进入静态块或方法时,jvm知道要采取哪个锁即类级别。 User has no control over it. 用户无法控制它。 So instance method will take object lock and static method will take class lock as thread works on it irrespective of how call is made. 因此,实例方法将采用对象锁定,并且静态方法将采用类锁定作为线程在其上工作,而不管如何进行调用。

Q1: you can reed the following link Q1:您可以使用以下链接

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Q2: First of all the second method will show up a warning that it should declared as static otherwise the field should not be static. Q2:首先,第二种方法将显示一个警告,它应该声明为静态,否则该字段不应该是静态的。 How ever they Absolutely have different locks; 他们怎么绝对拥有不同的锁; Instance method have object lock and static object have class level lock 实例方法有对象锁,静态对象有类级锁

暂无
暂无

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

相关问题 当我们在java中创建Integer类的实例时会发生什么 - What happens when we create instance of Integer class in java 当我们明确调用Garbage collector方法时会发生什么? - What happens when we explicitly call Garbage collector method? 当我们调用ArrayList.add()方法时会发生什么? - What happens when we call ArrayList.add() method? 当我们在 onCreate() 方法中调用 finish() 时会发生什么? - What happens when we call finish() in onCreate() method? 当使用空对象引用调用静态方法时会发生什么? - What happens when a static method is invoked using a null object reference? 是第一次调用静态方法时自动创建的类的实例 - Is an instance of a class automatically created when you first call a static method Java编译器会优化对最终静态变量的方法调用吗? 当它成为达尔维克代码时会发生什么? - Will java compiler optimize a method call on a final static variable ? And what happens when it becomes dalvik code? 当两个线程同时调用相同的静态方法时会发生什么? - What happens when two threads call the same static method at the same time? 我们如何用Java称呼一个类的实例 - What do we call an instance of a class in Java 在另一个函数中创建子类的实例时,为什么不能从子类类型的 Abstact 类调用方法(公共或静态)? - Why can't I call a method (public or static) from an Abstact class of type subclass when creating an instance of the subclass in another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM