Passing variables in a Java program

3

I am learning how to program and I have a problem with my public static void main(String[] args) {... I have a variable that within this procedure changes the value of that variable I need to use it in another class. Example: the variable "c" I need to occupy in another class the same package

public class mishel {
    private static int c;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a, b;
        a=8;
        b=9;
        c=a+b;
        System.out.println(c);

    }
    public void set_c(int var1){ 
        this.c=var1; 
    } 

    public int getc(){ 
        return this.c; 
    } 

}

/////////////////////////////////////////////// //////////

In my other class I have

public class programa {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        mishel c1=new mishel(); 
        c1.getc(); 
        int nc=c1.getc(); 
        int d = nc;
        System.out.println(d);

    }
}

But it prints me 0 .

    
asked by Mishel 20.10.2016 в 13:23
source

3 answers

1

I do not recommend writing your code in the main function, at first it may seem easier, but getting used to it is bad because it has several limitations (like the need to use Static variables). But this is not relevant now, let's go to the subject, I would do it like this:

public class MiClase {

    public int c;

    /*
     * Esto es un constructor, se le llama automaticamente cuando se crea un objeto.
     */
    public MiClase() {
        int a=8, b=9;
        c = a + b;
    }
}

Second class:

public class programa {
    public static void main(String[] args) {

        // Creo un objeto de nombre mc y tipo MiClase.
        MiClase mc = new MiClase();

        System.out.println(mc.c);
    }
}

The key here are these two commands:

  

MyClass mc = new MyClass ();

Here what we do is create an object called "mc" of type MiClase. In this case, I created a public variable of type int called "c" in the MyClass class, so mc has that variable.

I added the sum in the constructor, but it's not necessary either, let's leave it that way so that it's simpler for the moment.

  

mc.c

When I created the object "mc", inside the global variable "c" has been created, and to access its value (It can already be public) mc.c is used, in my example what I do is write it on the screen, but you can use it however you want.

If you have any questions, please, ask. We will be happy to help.

Update: Getter and Setter :

To avoid problems with public variables (These can be used from other classes, and that can bring problems if you do not control more complex programs), private variables and public "getter" and "setter" functions are used . Basically, functions that overwrite (save) or read the private variable. (Since it is private you can not access it (NEITHER read or write) from outside the class.

This would look like this:

public class MiClase {

    private int c;

    /*
     * Esto es un constructor, se le llama automaticamente cuando se crea un objeto.
     */
    public MiClase() {
        int a=8, b=9;
        c = a + b;
    }
    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }
}
    
answered by 20.10.2016 в 13:55
0

The normal thing is to encapsulate the operations to be carried out, for example using a class to make that sum.

Program class code

This program class is the one that carries the main to be executed directly.

package paqueteDePrueba;

public class Programa {

    public static void main(String[] args) {

        int a, b, c;
        a=8;
        b=9;

        ClaseSumadora cs = new ClaseSumadora();
        c =  cs.sumar(a,b);

        System.out.println(c);

        //si necesitaras usar 'c' en alguna otra clase
        NuevaClase miNuevaClase = new NuevaClase();
        miNuevaClase.hacerOperacionConElSumatorio(c);

        //o invocar un set
        //miNuevaClase.setSumatorio(c);

        //o construir la nueva clase empleando c
        //NuevaClase miNuevaClaseConstruidaConC = new NuevaClase(c);            
    }    
}

Sumadora class code

Only perform the addition operation.

public class ClaseSumadora{

    //obtiene la suma 'c'
    public int sumar(int a, int b){
        return a + b; 
    }
}
    
answered by 20.10.2016 в 14:10
0

It's very simple. The main method of the mishel class is not running. The main method will only be executed if you run that class as a java application.

When creating a mishel type object from the program class, only the constructor of that class is executed. As you have not defined it explicitly, the default constructor is executed, which is empty. since c is a primitive variable, its value is equal to 0, which is the value that is printing to you.

What you should do is create a constructor for the mishel class and copy the code from the main method.

    
answered by 20.10.2016 в 16:26