Import a. java into another . java

Asked

Viewed 569 times

0

I have 2 codes:

Code 1:

class Principal {

public static void main(String[] args){

Pessoas pessoa1 = new Pessoas();
pessoa1.idade = 1;
pessoa1.nascimento = 2;
pessoa1.altura = 0.5;
pessoa1.nome = "texto";


}
}

Code 2:

class Pessoas {

int idade;
int nascimento;
double altura;
String nome;

public static void main(String[] args){


}
}

I’ve tried using the package and import, but it didn’t work. I think I used them wrong. How do I use them to use code 2 in code 1?

NOTE: The two codes are in the same folder.

  • Yes, the 2 codes are in the same folder and I need the code 1 to access the code 2 to create the personal data1

  • Worked normally with me.

  • error here saying that Pessoas pessoa1 = new Pessoas(); says it is not possible to find the symbol People

  • So you’re not putting them both in the same package.

  • the 2 are in the same folder and the code is exactly as I put in the post

  • As I said, I simulated here and gave nothing, this error occurs when a class is not oncocentered. If you were both in the same package, you wouldn’t make this mistake.

  • how do I put the 2 in the same package? to see if this error goes missing

  • If you are using eclipse or netbeans, just drag the class to the same package as the other one is.

  • I don’t use any of the 2 create the code using Notpad++, I can’t use any of the 2

  • No ide, just put the files in the same folder.

  • then I’m using in the same folder put in the cmd says that Pessoas pessoa1 = new Pessoas();it is not possible to find the sinbulo People

  • Well, then I don’t know how to help you. Because I simulated creating in the ide, then manually creating in the Notepad and cmd, and in both ways it worked normally here. The problem is in another procedure you are doing.

  • What command are you using to compile these files?

  • javac "filename". java

  • @Clockwork You are compiling which file first?

  • Enter the entire code, including the Imports.

  • @Pabloalmeida however incredible it may seem, the code is just that, there are no Mports. If you save both, normal wheel, I believe this question is a XY problem.

  • @Clockwork Put the exact command you are using to compile. If you have more than 1 class to compile you have to compile all of them, for example javac *.java

Show 13 more comments

3 answers

1

You have to compile the code like this:

javac Principal.java Pessoas.java

Then, to rotate, do this:

java Principal

It is very important to have both files on the command line of javac, otherwise it will not compile.

0

If both are in the same folder, then they should be in the same package (default access as you declared the class)

Try putting in different folders and declaring both classes as "public class"

  • The fact that they do not have the public modifier also does not cause the error. Try creating the two files without public and compile for you to see. My conclusion is that, after testing the author’s codes, the problem has nothing to do with any of the codes.

  • Truth, I was thinking, it would only be a problem even if the two classes were default and were in different packages, then it would not have visibility, but I think that is not the case.

-2

In class 2, one should not have a main method but a constructor:

public Pessoas(){
}

In class 1, use above the class:

import Pessoas;
  • Still Returns Class1.java:8: error: cannot find Symbol People persons1 = new People();

  • I think this has nothing to do with the problem. The code works normally without the constructor. Any nonabstract class is instantiative without having a explicitly stated constructor

  • 1

    Look, there’s no impediment to having multiple functions main in a Java project... not to mention that it is in the same package so does not need explicit import. And as @Articuno said, the default constructor has nothing to do with

Browser other questions tagged

You are not signed in. Login or sign up in order to post.