Foros aprenderaprogramar.com

Aprender a programar => Aprender a programar desde cero => Mensaje iniciado por: mily en 31 de Marzo 2014, 19:01

Título: Non-static variable cannot be referenced from a static context java
Publicado por: mily en 31 de Marzo 2014, 19:01
tengo 3 clases en el paquete exercise12 ,me aparece un error en la clase testdriver y no se por que en las otras clases no tengo ningun error :
package exercise12;


public class TestDriver {
Time time1;
Time time2;
   public static void main(String[] args) {
      
      
      // compute the duration from 1:20:30 to 2:30:40
      time1 = new Time(1, 20, 30);
      time2 = new Time(2, 30, 40);
      System.out.println("time1    = " + time1 + "\ntime2    = " + time2);
      System.out.println("duration = " + time1.computeDuration(time2));
      System.out.println();
      
      // compute the duration from 1:45:10 to 2:30:05
      time1 = new Time(1, 45, 10);
      time2 = new Time(2, 30, 05);
      System.out.println("time1    = " + time1 + "\ntime2    = " + time2);
      System.out.println("duration = " + time1.computeDuration(time2));
      System.out.println();

      WorldTime worldTime1;
      WorldTime worldTime2;
      
      // compute the duration from 1:20:30 GMT+0 to 2:30:40 GMT+3
      worldTime1 = new WorldTime(1, 20, 30, 0);
      worldTime2 = new WorldTime(2, 30, 40, 3);
      System.out.println("worldTime1 = " + worldTime1 + "\nworldTime2 = " + worldTime2);
      System.out.println("duration   = " + worldTime1.computeDuration(worldTime2));
      System.out.println();
      
      // compute the duration from 1:45:10 GMT+0 to 2:30:05 GMT+3
      worldTime1 = new WorldTime(1, 45, 10, 0);
      worldTime2 = new WorldTime(2, 30, 05, 3);
      System.out.println("worldTime1 = " + worldTime1 + "\nworldTime2 = " + worldTime2);
      System.out.println("duration   = " + worldTime1.computeDuration(worldTime2));
   }
}
Título: Re:ayuda con netbeans,tengo este error y no se porque
Publicado por: César Krall en 31 de Marzo 2014, 21:10
Hola mily, para poder revisarlo pega el código de las otras clases, si no no se puede compilar. Para pegar el código, usa el botón # que aparece en el editor del foro y pega el código entre las etiquetas [ code ] ... [ / code] que te aparecerán (comprueba que se muestra como código pulsando el botón previsualizar antes de darle al botón publicar). Saludos.
Título: Re:ayuda con netbeans,tengo este error y no se porque
Publicado por: mily en 01 de Abril 2014, 07:39
Código: [Seleccionar]
package exercise12;


public class Time {
int hours;
int minutes;
int seconds;

/**
* Creates a Time object with the given hours, minutes, and seconds.
*/
public Time(int hours, int minutes, int seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}

/**
* Returns the time duration between this time and the given nextTime.
*/
public Time computeDuration(Time nextTime) {

// convert this time and nextTime to seconds
int secs = (3600 * hours) + (60 * minutes) + seconds;
int nextSecs = (3600 * nextTime.hours) + (60 * nextTime.minutes) +
nextTime.seconds;

// compute duration in seconds
int durationSecs = Math.abs(nextSecs - secs);

// convert duration seconds to hours, minutes, seconds
int h = durationSecs / 3600;
durationSecs = durationSecs % 3600;  // remainder of seconds in an hour
int m = durationSecs / 60;
int s = durationSecs % 60;

// create duration as Time object and return it
Time duration = new Time(h, m, s);
return duration;
}

/**
* Returns this time in the format "hours:MM:SS"
*/
public String toString() {
String s = hours + ":";
if (minutes < 10) s += "0";
s += minutes + ":";
if (seconds < 10) s += "0";
s += seconds;
return s;
}
}

Código: [Seleccionar]
package exercise12;


public class WorldTime extends Time {
int gmtOffsetHours;

/**
* Creates a WorldTime object with the given hours, minutes, seconds, and
* gmtOffsetHours.
*/
public WorldTime(int hours, int minutes, int seconds, int gmtOffsetHours) {
super(hours, minutes, seconds);
this.gmtOffsetHours = gmtOffsetHours;
}

/**
* Returns this world time in the format "hours:MM:SS GMT+h" where h is the
* number of hours offset from GMT.
*/
public String toString() {
return super.toString() + " GMT+" + gmtOffsetHours;
}
}
Título: Re:ayuda con netbeans,tengo este error y no se porque
Publicado por: César Krall en 01 de Abril 2014, 09:15
El mensaje de error que da es "Non-static variable time1 cannot be referenced from a static context" que indica que has declarado la variable como una variable de clase pero la estás inicializando en un método estático.

Para que te compile basta con declarar la variable dentro del método main así:

Código: [Seleccionar]
public class TestDriver {
    public TestDriver (){
    }
    public static void main(String[] args) {
        Time time1;
        Time time2;
        // compute the duration from 1:20:30 to 2:30:40
        time1 = new Time(1, 20, 30);
        time2 = new Time(2, 30, 40);
        System.out.println("time1    = " + time1 + "\ntime2    = " + time2);
        System.out.println("duration = " + time1.computeDuration(time2));
        System.out.println();

        // compute the duration from 1:45:10 to 2:30:05
        time1 = new Time(1, 45, 10);
        time2 = new Time(2, 30, 05);
        System.out.println("time1    = " + time1 + "\ntime2    = " + time2);
        System.out.println("duration = " + time1.computeDuration(time2));
        System.out.println();

        WorldTime worldTime1;
        WorldTime worldTime2;

        // compute the duration from 1:20:30 GMT+0 to 2:30:40 GMT+3
        worldTime1 = new WorldTime(1, 20, 30, 0);
        worldTime2 = new WorldTime(2, 30, 40, 3);
        System.out.println("worldTime1 = " + worldTime1 + "\nworldTime2 = " + worldTime2);
        System.out.println("duration   = " + worldTime1.computeDuration(worldTime2));
        System.out.println();

        // compute the duration from 1:45:10 GMT+0 to 2:30:05 GMT+3
        worldTime1 = new WorldTime(1, 45, 10, 0);
        worldTime2 = new WorldTime(2, 30, 05, 3);
        System.out.println("worldTime1 = " + worldTime1 + "\nworldTime2 = " + worldTime2);
        System.out.println("duration   = " + worldTime1.computeDuration(worldTime2));
    }
}
Título: Re:ayuda con netbeans,tengo este error y no se porque
Publicado por: mily en 01 de Abril 2014, 20:59
ok muchas gracias¡¡  ;D