[英]How to Access an object created in main method
I'm struggling a bit finding how to send/access an object that has been created in the main method by another static method. 我正在努力寻找如何发送/访问由main方法在另一个静态方法中创建的对象的方法。
Here is my some of my service class code, which consists of public constructor, accessor and mutator methods: 这是我的一些服务类代码,由公共构造函数,访问器和更改器方法组成:
public class Participants
{
String firstName=" ";
String lastName=" ";
int socialSecurity=0;
int ticket=0;
char status='N';
Vehicle veh=null;
public Participants(String fn, String ln, int ssn, int tkt)
{
firstName=fn;
lastName=ln;
socialSecurity=ssn;
ticket=tkt;
}
}
And there is the Client class, which has the main method where I created and initialized those objects and second method which I'm trying to access those objects: 还有Client类,它具有创建和初始化那些对象的主要方法以及试图访问这些对象的第二种方法:
public class Race
{
public static void main(String[] args)
{
....
Participants []people=new Participants[35];
Vehicle []cars=new Vehicle[10];
...code to "fill" those objects
GiveAway([]cars,[]people); //sending those objects to static method- doesn't work from here)
}
public static void GiveAway(Vehicle[] veh, Participants[] part)
{
//set of instructions to work and change those objects
}
}
The code simply doesn't work and it is because I don't really know how to either "send" an object array to an method (is that possible, by the way?). 该代码根本行不通,这是因为我真的不知道如何将对象数组“发送”到方法中(顺便说一句,这有可能吗?)。
Am I doing it the right way? 我做对了吗? Or there is an easier way around? 还是有更简单的方法? I found some subjects about private classes and everything else, but couldn't figure out what to do with my code. 我发现了一些有关私有类以及其他所有内容的主题,但无法弄清楚如何处理我的代码。
I appreciate any help 感谢您的帮助
Thank you! 谢谢!
I think that you believe that the array name is []people
and []cars
. 我认为您相信数组名称是[]people
和[]cars
。 It's not. 不是。 When you declare them, it's actually: 当您声明它们时,实际上是:
Vehicle[] cars = new Vehicle[10];
└───────┘ └──┘
Type var name
So the array is named cars
, and that's how you should pass it to the other method: 因此,该数组被命名为cars
,这就是将其传递给另一个方法的方式:
GiveAway(cars, people);
As a side note: don't give methods names that begin with a capital letter. 附带说明:不要给方法名称以大写字母开头。 The conventions are that only type names (classes, interfaces, etc) begin with a capital letter. 约定只有类型名称(类,接口等)以大写字母开头。 Constants are all-caps, and methods have a lowercase first letter. 常量全为大写,方法的首字母为小写。
Your call to GiveAway
should look like this 您给GiveAway
电话应如下所示
GiveAway(cars, people);
Those square brackets are giving you a compiler error. 这些方括号给您一个编译器错误。
Participants []people=new Participants[35];
Vehicle []cars=new Vehicle[10];
GiveAway([]cars,[]people);
should be 应该
Participants[] people=new Participants[35];
Vehicle[] cars=new Vehicle[10];
GiveAway(cars, people);
In Java you use the [] to signal the compiler that this is an array. 在Java中,您可以使用[]通知编译器这是一个数组。 you put it right behind the name of the Object you want to create an array of (Participants, Vehicle). 您将其放在要创建(参与者,车辆)数组的对象名称的后面。 And when calling "GiveAway" you only need to use the names of the arrays. 当调用“ GiveAway”时,您只需要使用数组的名称即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.