在java中调用非静态方法之前创建一个实例 [英] making an instance before calling non static method in java

查看:90
本文介绍了在java中调用非静态方法之前创建一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以请某人向我解释为什么在调用java中的main函数的非静态方法之前必须创建一个实例?这背后的原因是什么?

Hi could someone please explain to me why you have to create an instance before calling a non static method to the main function in java? What is the reasoning behind this?

推荐答案

因为,他们是实例成员,要访问它们,你需要实例。

Because, they are instance members,to access them you need instance.


当从同一个类蓝图创建多个对象时,它们每个都有自己不同的实例变量副本。在Bicycle类的情况下,实例变量是节奏,齿轮和速度。每个Bicycle对象都有自己的变量值,存储在不同的内存位置。

When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations.

所以现在关于<$ c $的第二个问题c> static


有时,您希望拥有所有对象共有的变量。这是通过静态修改器完成的。在声明中具有static修饰符的字段称为静态字段或类变量。它们与类相关联,而不是与任何对象相关联。该类的每个实例共享一个类变量,该变量位于内存中的一个固定位置。任何对象都可以更改类变量的值,但也可以在不创建类实例的情况下操作类变量。

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

< a href =http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html =nofollow>了解实例和班级成员

这篇关于在java中调用非静态方法之前创建一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆