Import one .java into another .java [closed]

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 to use package and import , but it did not work. I think I used them wrong. How do I use them to use code 2 in code 1?

NOTE: Both codes are in the same folder.

    
asked by anonymous 22.10.2017 / 18:12

3 answers

1

You have to compile the code like this:

javac Principal.java Pessoas.java

Then, to run, do this:

java Principal

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

    
29.10.2017 / 18:40
0

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

Try to put in different folders and declare both classes as "public class"

    
23.10.2017 / 20:58
-2

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

public Pessoas(){
}

Already in class 1, use above the class:

import Pessoas;
    
24.10.2017 / 00:39