本文介绍了什么是Java中的变量,为什么要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在创建一个对象,它应该被另一个方法使用。 我如何实现这个目标? 这是我的代码: public class Program { public static void main(String [ ] args){ new MyClass(); myMethod(); } public static void myMethod(){ //这里我想用我的对象} } 公共类MyClass { } 解决方案您应该了解两个关键要素: 使用变量来存储对象和 然后将其作为参数传递给方法。 第1部分:变量 创建对象后,应将其存储在变量中。否则它只是存在于内存中,将被垃圾收集,因此消失。拥有一个变量使您可以在以后访问它,并且只要该变量存在就会阻止它被垃圾收集(这是关于范围的东西,您可能也想让自己熟悉它)。 变量有一个类型( MyClass )和一个名字,你使用 = 分配它。 以下是相关部分: public static void main(String [] args){ MyClass object1 = new MyClass(); //< ---注意改变 myMethod(); } 您现在有一个名为 object1的变量并且您可以使用该名称。 第2部分:参数 现在你有一个变量,你可以给它给方法。我们也说通过。这在括号中完成,如下所示: public static void main(String [] args){ MyClass object1 = new MyClass(); myMethod(object1); } 此时,您的代码将无法运行,因为该方法不知道关于参数的任何事情。根据工具的不同,您可能会立即看到此信息,例如:通过Eclipse中带下划线的文本: I am creating an object and it should be used by another method.How do I achieve this?Here's my code:public class Program { public static void main(String[] args) { new MyClass(); myMethod(); } public static void myMethod() { // Here's where I want to use my object }}public class MyClass {} 解决方案 There are two key elements you should know about:Use a variable to store the object andthen pass it as a parameter to a method.Part 1: a variableAfter creating an object, you should store it in a variable. Otherwise it just exists in memory and will be garbage collected and thus be gone. Having a variable enables you to access it later and it will prevent it from being garbage collected as long as the variable exists (that's something about scope, you might want to make yourself familiar with that as well).Variables have a type (MyClass) and a name and you use the = to assign it.Here's the relevant part:public static void main(String[] args) { MyClass object1 = new MyClass(); // <--- note the change myMethod();}You now have a variable named object1 and you can use it with that name.Part 2: a parameterNow that you have a variable, you can "give" it to the method. We also say "pass". This is done in the parentheses like this:public static void main(String[] args) { MyClass object1 = new MyClass(); myMethod(object1);}At this time, your code will not run, because that method does not know anything about the parameter yet. Depending on the tool, you may see this immediately, e.g. by underlined text in Eclipse:To tell the method what it gets, you also define the type and a name there:public static void myMethod(MyClass differentName) { // Here's where I want to use my object}Inside the method myMethod, note that the name of the variable can be different. This does not matter, the program just gives a different name to the exact same thing. My name is Alfred, but you can call me Al.Inside myMethod you can use the variable under its new name only. Depending on what you want to do, you can access it by typing its name and a .. You can even pass it to other methods. Depending on the tool, you'll get suggestions on what you can do with it: 这篇关于什么是Java中的变量,为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-01 17:00