简体   繁体   English

如何访问在main方法中创建的对象

[英]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.

相关问题 从另一个类访问在主类中创建的对象方法 - Access an object method created in main class from another class 如何在创建对象的方法之外访问对象数组 - How to access array of object outside the method it was created in 如何在主方法中访问A类的打印方法? 在main方法中创建的对象a和b之间的区别? - How to access print method of class A in main method? Difference between objects a and b created in main method? 如何使用在 Java 的主 class 中创建的 object 将值传递给方法 - How to pass value to a method using object created in main class in Java 如何从不同的方法访问以某种方法创建的对象? - How to access an object which is created in some method, from different method? 如何让我的主要方法访问在Applet中创建的paint方法? - How would I let my main method access the paint method I created inside Applet? 如何访问类的动态创建对象的方法和属性 - How to access method and properties of dynamically created Object for a class 让我困惑的基本事情:如何访问在主类的方法中创建的变量 - Basic thing thats confusing me: How to access a variable created in a method from the main class 从主外部的方法访问类对象 - Access a class object from a method outside the main 是否可以访问在方法内部创建的 object? - Is it possible to access an object created inside of a method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM