简体   繁体   中英

“.class expected” error when calling from main method from another class constructor

Im making a basic program that creates an object with certain attributes, and it works fine, but I need to load it as a independent program itself, so I created another class called Lanzador, which calls the constructor from the other class so that it can create the objects.

Im new to this so I dont really know what I'm doing here:

public class Lanzador
{
    public static void main(String args[]) {
        TipodeTirada tirada = new TipodeTirada(String,String,int,String,boolean,int,boolean);

    }
}

The problem is that I dont know how to really do this, since it still gives me an "int.class" expected.

What should I do so that when I start the program it lets me input the attributes (stirng, int, etc) ?

Thanks a lot.

You need to supply actual values to your constructor rather than type keywords. Passing in the keywords will only make the compiler complain as its expecting literal values. Instead you could use, (for example):

new TipodeTirada("some value", "value2", 100, "value 3" ,false, 200, true);
TipodeTirada tirada = new TipodeTirada("a","b",1,"c",false,2,true);

you need to send actual values to constructor

Note: "a","b" and all are dummy values put values that make sense in your scenario.

This :

TipodeTirada tirada = new TipodeTirada(String,String,int,String,boolean,int,boolean);

Doesn't exist in Java . Even when declaring a method, you will have to give a name to your parameters :

public void myFunction(String param1, int param2)
{
     ...
}

It does exist in C language but it's another problem.

Here, you want to create an instance of TipodeTirada , so you have to pass actual values when calling the method, for example :

TipodeTirada tirada = new TipodeTirada("String 1","String 2",1,"String 3",true,2,false);

Supposing you have a class TipodeTirada like this :

public class TipodeTirada {
    String name, surname, value;
    int age, weight;
    boolean bool1, bool2;
}

Then you'll have your constructor inside like this :

public TipodeTirada(String name,String surname,int age,String value,boolean bool1,int weight,boolean bool2)
{
    this.name = name;
    this.surname = name;
    // etc...
}

So what you're doing is creating a method that you're now calling inside your main that assigns your parameters values to your TipodeTirada's instance's fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM