Llevar el valor de una variable de una clase a otra clase
Publicado por Carlos Rodriguez (6 intervenciones) el 02/06/2012 16:20:15
Hola estoy haciendo un programa para mi clase y tengo un problema.
tengo dos classes una main estoy calculando unas comiciones pero necesito llevar ese valor de esa variable a la otra clase. hasta ahora solo lo he podido hacer haciendo un valor estatico pero como puedo hacerlo con una variable?
este es el main:
Este es el class:
Si uso link = Commision; me da error de que non-static variable cannot be referenced from a static context.
me pueden ayudar?
Gracias
tengo dos classes una main estoy calculando unas comiciones pero necesito llevar ese valor de esa variable a la otra clase. hasta ahora solo lo he podido hacer haciendo un valor estatico pero como puedo hacerlo con una variable?
este es el main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Neowolf
*/
import java.util.Scanner;
public class Week2 {
/**
* @param args the command line arguments
*
*/
double link;
public double showInfo()
{
return link;
}
public static void main(String[] args) {
// TODO code application logic here
Week2class myWeek2class = new Week2class();//call the class Week2class
Scanner input = new Scanner(System.in);//create the input variable from the Scanner
int AnnualSales;//declare variables
int Salary;
double Commision;
// Validate the input is a integer and a positive value
do {
System.out.println("Please enter a Annual Sales");
while (!input.hasNextInt()) {
System.out.println("That's not a valid value!");
input.next(); // this is important!
}
AnnualSales = input.nextInt();
} while (AnnualSales <= 0);
// Validate the input is a integer and a positive value
do {
System.out.println("Please enter a Salary");
while (!input.hasNextInt()) {
System.out.println("That's not a valid value!");
input.next(); // this is important!
}
Salary = input.nextInt();
} while (Salary <= 0);
Commision = Salary + (AnnualSales * .08);
link = Commision;
myWeek2class.displayMessage();
}
}
Este es el class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Neowolf
*/
public class Week2class {
public void displayMessage()
{
Week2 myCommision = new Week2();
System.out.print("Commision and Salary Total is $");
System.out.println(myCommision.showInfo());
}
}
Si uso link = Commision; me da error de que non-static variable cannot be referenced from a static context.
me pueden ayudar?
Gracias
Valora esta pregunta


0