Vous êtes sur la page 1sur 39

Código Java

OCA Java SE 8

package com.codemonkey;

import static java.lang.System.out;

import static java.lang.System.err;

import java.time.LocalDateTime;

import java.time.LocalTime;

import java.time.Month;

import java.time.Duration;

import java.time.LocalDate;

import java.time.Period;

import java.time.format.DateTimeFormatter;

import java.time.temporal.UnsupportedTemporalTypeException;

public class Main{

static public void main(String $[]){

getTimeAPIDemo();

static void getTimeAPIDemo(){

LocalDate localDate = LocalDate.now();

out.println(localDate);//2018-05-07

Period period = Period.parse("P2Y");

out.println(period.getMonths());//0
try{

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_DATE));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_DATE_TIME));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_INSTANT));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_
TIME));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_OFFSET_DATE)
);

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_
TIME));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_OFFSET_TIME))
;

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_ORDINAL_DAT
E));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_TIME));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_WEEK_DATE));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.ISO_ZONED_DATE_
TIME));

out.println(LocalDateTime.now().format(java.time.format.DateTimeFormatter.BASIC_ISO_DATE));

}catch(UnsupportedTemporalTypeException ute){

err.println("Excepcion: campo no soportado");

ute.printStackTrace();
}

localDate = LocalDate.of(1940, Month.JANUARY, 3);

out.println(localDate);//1940-01-03

localDate = localDate.minusYears(3).plusMonths(10).plusDays(5);

out.println(localDate);//1937-11-08

localDate.minusYears(10);//aqui no pasa nada

out.println(localDate);//1937-11-08

out.println(localDate.getMonth()

+" - "+localDate.getYear());//NOVEMBER - 1937

String date1 = "2011-12-03";

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

localDate = LocalDate.parse(date1, formatter1);

out.println(localDate.toString());//2011-12-03

//String date2 = "2014-12-03 00:00:00";//Esto no es correcto

String date2 = "2014-12-03T00:00:00";//Esto es correcto

DateTimeFormatter formatter2 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

localDate = LocalDate.parse(date2, formatter2);

out.println(localDate.toString());//2014-12-03

localDate = LocalDate.of(2019, Month.APRIL, 30);

out.println(localDate.toString());//2019-04-30

period = Period.parse("P2Y8M");

out.println(period.getYears());//2

out.println(period.getMonths());//8
out.println(period.getDays());//0

out.println(period.toTotalMonths());//32

Duration duration = Duration.ofSeconds(355);

out.println(duration.getSeconds());//355

duration = Duration.ofNanos(90909090);

out.println(duration.getNano());//90909090

out.println(duration.getSeconds());//0

LocalDateTime localDateTime = LocalDateTime.now().withYear(2019);

out.println(localDateTime);//2019-05-07T15:36:36.340

localDateTime = LocalDateTime.now().withMonth(11);

out.println(localDateTime);//2018-11-07T15:37:26.823

localDateTime = LocalDateTime.now().withHour(10);

out.println(localDateTime);//2018-05-07T10:38:05.398

localDateTime = LocalDateTime.now().withMinute(45);

out.println(localDateTime);//2018-05-07T15:45:45.689

localDateTime = LocalDateTime.now().withSecond(32);

out.println(localDateTime);//2018-05-07T15:40:32.238

period = Period.of(3,0,1);//of(año, mes, dia)

out.println(period.toString());//P3Y1D

period = Period.of(0,2,1);

out.println(period.toString());//P2M1D

period = Period.of(3,2,0);

out.println(period.toString());//P3Y2M

//Esto no se puede:

//period = Period.of(0,Month.DECEMBER,0);
period = Period.of(0,12,0);

out.println(period.toString());//P12M

period = Period.of(2017, 3, 1);

period = period.plusDays(1);

out.println(period.toString());//P2017Y3M2D

period = Period.of(2015, 3, 1);

period = period.plusYears(5);

out.println(period.toString());//P2020Y3M1D

period = Period.of(2018, 3, 1);

period = period.plusMonths(3);

out.println(period.toString());//P2018Y6M1D

period = Period.of(2018, 3, 1);

period = period.minusYears(3);

out.println(period.toString());//P2015Y3M1D

period = Period.of(2018, 3, 1);

period = period.minusMonths(2);

out.println(period.toString());//P2018Y1M1D

period = Period.of(2018, 3, 1);

period = period.minusDays(1);

out.println(period.toString());//P2018Y3M

LocalTime localTime = LocalTime.MAX;


out.println(localTime.toSecondOfDay());//86399

LocalTime localTimeTemp = LocalTime.MIDNIGHT;

localDateTime = localTimeTemp.atDate(LocalDate.now());

out.println(localDateTime);//2018-05-07T00:00

//18

/*

import static java.lang.System.out;

import static java.lang.System.err;

import java.time.Duration;

import java.time.Instant;

import java.time.LocalDate;

import java.time.Month;

import java.time.Period;

import java.time.temporal.TemporalAmount;

import java.time.DateTimeException;
public class Main{

static public void main(String $[]){

getPeriodDemo();

public static void getPeriodDemo(){

CharSequence charSequence = "P1D";

Period period = Period.parse(charSequence);

out.println(period);//P1D

int years = 2;

int months = 4;

int days = 120;

period = Period.of(years, months, days);

out.println(period);//P2Y4M120D

out.println(period.getYears());//2

out.println(period.getMonths());//4

out.println(period.getDays());//120

years = 3;

period = Period.ofYears(years);

out.println(period);//P3Y

months = 6;

period = Period.ofMonths(months);

out.println(period);//P6M

days = 15;

period = Period.ofDays(days);
out.println(period);//P15D

int weeks = 4;// 1 semana = 7 dias; 4 x 7 = 28 dias

period = Period.ofWeeks(weeks);

out.println(period);//P28D

TemporalAmount temporalAmount = Period.from(Period.ofMonths(12));//P12M

period = Period.from(temporalAmount);

out.println(period);//P12M

temporalAmount = Period.from(Period.ofYears(4));//P4Y

period = Period.from(temporalAmount);

out.println(period);//P4Y

temporalAmount = Period.from(Period.ofDays(120));//P120D

period = Period.from(temporalAmount);

out.println(period);//P120D

//1 semana = 7 dias ; 30 x 7 = 210 dias

temporalAmount = Period.from(Period.ofWeeks(30));//P210D

period = Period.from(temporalAmount);

out.println(period);//P210D

period = Period.parse("P1Y2M3D").plusYears(1).plusMonths(2).plusDays(3);

out.println(period);//P2Y4M6D

period = period.minusYears(1).minusMonths(2).minusDays(3);

out.println(period);//P1Y2M3D
Month mesA = Month.JULY;

Month mesB = Month.DECEMBER;

LocalDate localDateA = LocalDate.of(2010, mesA, 12);

LocalDate localDateB = LocalDate.of(2010, mesB, 12);

period = Period.between(localDateA, localDateB);

out.println(period);//P5M

mesA = Month.JULY;

mesB = Month.MARCH;

localDateA = LocalDate.of(2010, mesA, 12);

localDateB = LocalDate.of(2010, mesB, 12);

period = Period.between(localDateA, localDateB);

out.println(period);//P-4M

LocalDate today = LocalDate.now();

LocalDate birthday = LocalDate.of(1981, Month.DECEMBER, 18);

LocalDate nextBDay = birthday.withYear(today.getYear());

period = Period.between(today, nextBDay);

out.println(period);//P7M11D

Instant t1 = Instant.now();

out.println(t1);//2018-05-07T14:14:00.834Z

Instant t2 = Instant.now();

out.println(t2);//2018-05-07T14:14:00.852Z

long ns = Duration.between(t1, t2).toNanos();

out.println(ns);//18000000

try{
Period periodo = Period.parse("2010-02-12");

out.println(periodo);

}catch(DateTimeException | IllegalArgumentException e){

err.println("Excepcion en formato de periodo:");

e.printStackTrace();

//Salida:

//Excepcion en formato de periodo:

//java.time.format.DateTimeParseException: Text cannot be parsed to a Period

//at java.time.Period.parse(Period.java:350)

//...

LocalDate oldDate = LocalDate.of(1981, Month.DECEMBER, 18);

LocalDate newDate = LocalDate.now(); //LocalDate.of(2018, Month.MAY, 7);

out.println(oldDate);//1981-12-18

out.println(newDate);//2018-05-07

period = Period.between(oldDate, newDate);

out.print(period.getYears() + " años,");

out.print(period.getMonths() + " meses,");

out.print(period.getDays() + " dias\n");//36 años,4 meses,19 dias

period = Period.ZERO;

out.println(period);//P0D

*/
//17

/*

import static java.lang.System.out;

import static java.lang.System.err;

public class Main{

static public void main(String $[]){

java.util.List lista = new java.util.ArrayList<>();

lista.add(0,"D");

lista.add(1,"E");

lista.add(2,"L");

lista.add(3,"E");

lista.add(4,"T");

lista.add(5,"E");

java.util.Iterator it = lista.iterator();

try{

while(it.hasNext()){

it.next();

it.remove();

it.remove();

}catch(NullPointerException | IllegalStateException e){

err.println("Excepcion ocurrida: "+e.getMessage());

e.printStackTrace();

*/
//16

/*

import static java.lang.System.out;

import static java.lang.System.err;

public class Main{

static public void main(String $[]){

Vehiculo auto = new Auto("3W", 200,"Chocolates");

out.println("Type:"+auto.getType());

out.println("Maxima velocidad:"+auto.getMaxSped());

//Error de compilacion: getTrans() no pertenece a Vehiculo

//out.println("Transporte:"+auto.getTrans());

class Vehiculo{

private String type = "4W";

private int maxSpeed = 100;

Vehiculo(String type, int maxSpeed){

this.type=type;

this.maxSpeed=maxSpeed;

public String getType(){

return type;

}
public void setType(String type){

this.type=type;

public int getMaxSped(){

return maxSpeed;

public void setMaxSped(int maxSpeed){

this.maxSpeed=maxSpeed;

class Auto extends Vehiculo{

private String trans;

//Esto no compilara:

//Auto(String trans){

// this.trans=trans;

//}

Auto(String type, int maxSpeed, String trans){

super(type,maxSpeed);

this.trans=trans;

public String getTrans(){

return trans;

}
public void setTrans(String trans){

this.trans=trans;

*/

//15

/*

import static java.lang.System.out;

import static java.lang.System.err;

public class Main{

static public void main(String $[]){

abstract class Planeta{

protected void resolve(){}

abstract void rotate();

class Tierra extends Planeta{

//Error: no compilara, no se puede sobreescribir un metodo protected

//void resolve(){}
//Correcto:

public void resolve(){}

protected void rotate(){}

*/

//14

/*

import static java.lang.System.out;

import static java.lang.System.err;

public class Main{

static public void main(String $[]){

try{

Short s1 = 200;

Integer s2 = 400;

Long s3 = (long) s1 + s2;//600

String s4 = (String) (s3 * s2);//Error de compilacion

out.println("Suma:"+s4);

}catch(ClassCastException | NumberFormatException nfe){

err.println("Exception gnral.:"+nfe.getMessage());

nfe.printStackTrace();

}catch(Exception e){

err.println("Exception:"+e.getMessage());

e.printStackTrace();

}
}*/

//13

/*

import static java.lang.System.out;

import static java.lang.System.err;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

import java.time.temporal.UnsupportedTemporalTypeException;

public class Main{

static public void main(String $[]){

CharSequence charSequence = "2018-05-04";

try{

String date = LocalDate.parse(charSequence).format(DateTimeFormatter.ISO_DATE_TIME);

out.println(date);

}catch(ArrayIndexOutOfBoundsException | UnsupportedTemporalTypeException ute){

err.println("Ha ocurrido un error en la fecha: "+ute.getMessage());

ute.printStackTrace();

*/

//Salida:

//Ha ocurrido un error en la fecha: Unsupported field: HourOfDay

//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay

//...
//12

/*

import static java.lang.System.out;

public class Main{

static public void main(String $[]){

int a = 9;

if(a++ < 10){

//a sigue valiendo 9, 9 < 10

out.println("Hola, Fer!");

}else{

out.println("Hola, Adela!");

int b = 2;

if(++b <= 2){

//3 <= 2, no se ejecutara

out.println("Hola, Fer!");

}else{

out.println("Hola, Adela!");

}*/

//Salida:

//Hola, Fer!

//Hola, Adela!
//11

/*

import static java.lang.System.out;

public class Main{

static public void main(String $[]){

Producto prd = new Producto();

prd.precio = 200;

double newPrecio = 100;

Test test = new Test();

//test.actualizaPrecio(200, 100)

test.actualizaPrecio(prd, newPrecio);

out.println(prd.precio+" : "+newPrecio);//400 : 100

//Salida:

//400.0 : 100.0

class Producto{

double precio;

class Test{

public void actualizaPrecio(Producto producto, double precio){

precio = precio * 2;//100 * 2 = 200

producto.precio = producto.precio + precio;//200 + 200 = 400


}

*/

//10

/*

import static java.lang.System.out;

public class Main{

static public void main(String $[]){

Ejemplo ejemplo = new Ejemplo();

X objY = new Y();

ejemplo.metodo(objY);//Hola, desde Y

//Salida:

//Hola, desde Y

class X extends Object{

void metodo(){

out.println("Hola, desde X");

class Y extends X{
@Override

void metodo(){

out.println("Hola, desde Y");

class Ejemplo{

void metodo(X objX){

objX.metodo();

void metodo(Y objY){

objY.metodo();

}*/

//9

/*

import static java.lang.System.out;

import static java.lang.System.err;

public class Main{

static public void main(String $[]){

Object obj1 = "1";

Object obj2 = new String("null");


Object obj3 = null;

Object obj4 = 0;//int, Integer, short, Short

Object obj5 = '\u009e';//char, Character

Object obj6 = 78.0F;//float, Float

Object obj7 = new Boolean("true");//boolean, Boolean

Object obj8 = 127;//byte , Byte

Object obj9 = 120000L;//long, Long

Object obj10 = 123.000000;//double, Double

tryEntero(obj1);

tryEntero(obj4);

tryString(obj1);

tryString(obj2);

tryString(obj3);

static void tryString(Object obj){

try{

String string = (String) obj;

out.println("String:"+string);

}catch(ClassCastException | NullPointerException e2){

err.println("Excepcion ocurrida por:"+e2.getCause());

e2.printStackTrace();

static void tryEntero(Object obj){


try{

Integer entero = (Integer) obj;

out.println("Entero:"+entero);

}catch(ClassCastException | NumberFormatException e1){

err.println("Excepcion ocurrida por:"+e1.getCause());

e1.printStackTrace();

}*/

//Salida:

//Excepcion ocurrida por:null

//java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

// at com.codemonkey.Main.tryEntero(Main.java:32)

// at com.codemonkey.Main.main(Main.java:24)

//Entero:0

//String:1

//String:null

//String:null

//8

/*

import static java.lang.System.out;

import static java.lang.System.err;

import java.util.Random;

public class Main{


static public void main(String $[]){

Main.tryRandom();

protected static void tryRandom(){

try{

Random random = new Random();

int n = random.nextInt(3);

if(n == 0){

throw new ArrayIndexOutOfBoundsException();

}else{

throw new NullPointerException();

}catch(ArrayIndexOutOfBoundsException | NullPointerException e){

err.println("Ha ocurrido una excepcion:"+e.getMessage());

e.printStackTrace();

}*/

//Salida:

//Ha ocurrido una excepcion:null

//java.lang.ArrayIndexOutOfBoundsException

// at com.codemonkey.Main.tryRandom(Main.java:22)

// at com.codemonkey.Main.main(Main.java:14)

//7

/*

import static java.lang.System.out;

import static java.lang.System.err;


import static java.lang.System.in;

import java.util.InputMismatchException;

import java.util.Scanner;

public class Main{

static public void main(String $[]){

Main.tryScanner();

protected static void tryScanner(){

try(Scanner sc = new Scanner(in)){

out.println("Numero del mes del año:");

int numero=sc.nextInt();

out.println("Faltan "+(12-numero)+" para que termine el año");

}catch(InputMismatchException ime){

err.println("Una excepcion en la entrada de datos:"+ime.getCause());

ime.printStackTrace();

*/

//6

/*

import static java.lang.System.out;

public class Main{


static public void main(String $[]){

new Main().tryTestA();

Main.tryTestB();

public void tryTestA(){

try{

out.println("Hi!");

}finally{

out.println("Ciao!");

public static void tryTestB(){

out.println("A");

try{

out.println("B");

new NumberFormatException();

out.println("C");

}finally{

out.println("D");

out.println("E");

}*/

//Salida:
//Hi!

//Ciao!

//A

//B

//C

//D

//E

//5

/*

import static java.lang.System.out;

import static java.lang.System.err;

import static java.lang.System.exit;

import java.util.ArrayList;

public class Main{

static public void main(String $[]){

Main.tryEsEntero();//tambien puede ser asi: tryEsEntero()

tryCatchClassCast();

Main.tryArrayIndex();

tryTestA();//Formato del numero incorrecto

tryTestB();//El argumento no es valido

Main.tryTestC();//Hola

public static void tryEsEntero(){

try{
int numero = Integer.parseInt("223R");

}catch(NumberFormatException nfe){

err.println("Hay un problema con el formato del numero:"+nfe.getMessage());

nfe.printStackTrace();

}catch(IllegalArgumentException iae){

err.println("Hay un problema:"+iae.getMessage());

iae.printStackTrace();

catch(RuntimeException re){

err.println("Hay un problema:"+re.getMessage());

re.printStackTrace();

catch(Exception e){

err.println("Hay un problema:"+e.getMessage());

e.printStackTrace();

public static void tryCatchClassCast(){

ArrayList listaEnteros = new ArrayList();

listaEnteros.add(0,"21");

listaEnteros.add(1,"33");

listaEnteros.add(2,"111");

try{

Integer entero = (Integer)listaEnteros.get(1);

}catch(ClassCastException cce){

err.println("Ha ocurrido una excepcion con el \"casteo\":"+cce.getMessage());

cce.printStackTrace();

}catch(IllegalArgumentException iae){
err.println("Ha ocurrido una excepcion:"+iae.getMessage());

iae.printStackTrace();

catch(RuntimeException re){

err.println("Ha ocurrido una excepcion:"+re.getMessage());

re.printStackTrace();

}catch(Exception e){

err.println("Ha ocurrido una excepcion:"+e.getMessage());

e.printStackTrace();

public static void tryArrayIndex(){

int[] arreglo [] = {{2,5,4},{0,-3,-3},{-1,7,-3}};

try{

int entero = arreglo[0][3];

}catch(ArrayIndexOutOfBoundsException aie){

err.println("Tratas de acceder a un indice inexistente:"+aie.getMessage());

aie.printStackTrace();

}catch(NumberFormatException nfe){

err.println("El numero no tiene formato correcto:"+nfe.getMessage());

nfe.printStackTrace();

}catch(IllegalArgumentException iae){

err.println("El argumento es incorrecto:"+iae.getMessage());

iae.printStackTrace();

}catch(RuntimeException re){

err.println("Excepcion en tiempo de ejecucion:"+re.getMessage());

re.printStackTrace();

}
}

public static void tryTestA(){

try{

throw new NumberFormatException();

}catch(NumberFormatException nfe){

err.println("Formato del numero incorrecto");

public static void tryTestB(){

try{

throw new IllegalArgumentException();

}catch(IllegalArgumentException iae){

err.println("El argumento no es valido");

public static void tryTestC(){

try{

out.println("Hola");

exit(0);//El programa termina, ya no se ejecuta ninguna otra sentencia

}finally{

out.println("Adios");

}*/
//Salida:

//Hay un problema con el formato del numero:For input string: "223R"

//java.lang.NumberFormatException: For input string: "223R"

// at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

// at java.lang.Integer.parseInt(Integer.java:580)

// at java.lang.Integer.parseInt(Integer.java:615)

// at com.codemonkey.Main.tryEsEntero(Main.java:23)

// at com.codemonkey.Main.main(Main.java:14)

//Ha ocurrido una excepcion con el "casteo":java.lang.String cannot be cast to java.lang.Integer

//java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

// at com.codemonkey.Main.tryCatchClassCast(Main.java:47)

// at com.codemonkey.Main.main(Main.java:15)

//Tratas de acceder a un indice inexistente:3

//java.lang.ArrayIndexOutOfBoundsException: 3

// at com.codemonkey.Main.tryArrayIndex(Main.java:67)

// at com.codemonkey.Main.main(Main.java:16)

//Formato del numero incorrecto

//El argumento no es valido

//Hola

//4

/*

import static java.lang.System.out;

import static java.lang.System.err;

public class Main{

static public void main(String $[]){


String archivo = "archivo.xml";

GeneradorPDF generadorPDFA = new GeneradorPDF();

try{

generadorPDFA.setArchivo(archivo);

generadorPDFA.generadorPDF();

}catch(MyExcepcionA | NullPointerException e1){

err.println("Ha ocurrido una excepcion de tipo:"+e1.getCause());

e1.printStackTrace();

String doc = "documento.docx";

GeneradorXML generadorXML = new GeneradorXML();

try{

generadorXML.GeneradorXML(doc);

}catch(MyExcepcionB | ArrayIndexOutOfBoundsException e2){

err.println("Ha ocurrido una excepcion generando XML, causa:"+e2.getCause());

e2.printStackTrace();

//Salida:

//GeneradorPDF

//Generando PDF...

//Ha ocurrido una excepcion generando XML, causa:null

//com.codemonkey.MyExcepcionB: El archivo no debe tener extension *.docx

// at com.codemonkey.GeneradorXML.GeneradorXML(Main.java:66)

// at com.codemonkey.Main.main(Main.java:24)
class GeneradorPDF {

private String archivo;

GeneradorPDF(){

out.println("GeneradorPDF");

GeneradorPDF(String archivo){

super();

this.archivo=archivo;

public String getArchivo(){

return archivo;

public void setArchivo(String archivo){

this.archivo=archivo;

public void generadorPDF() throws MyExcepcionA{

if(this.archivo == null){

throw new MyExcepcionA("El archivo no puede ser null");

out.println("Generando PDF...");

class GeneradorXML {
void GeneradorXML(String nombre)throws MyExcepcionB{

if(nombre.endsWith(".docx")){

throw new MyExcepcionB("El archivo no debe tener extension *.docx");

out.println("Generando XML...");

class MyExcepcionA extends Exception{

public MyExcepcionA(){

super();

public MyExcepcionA(String msg){

super(msg);

class MyExcepcionB extends Exception{

public MyExcepcionB(){

super();

public MyExcepcionB(String msg){

super(msg);

*/
//3

/*

import static java.lang.System.out;

import static java.lang.System.err;

public class Main{

static public void main(String $[]){

String archivo = null;

GeneradorPDF generadorPDFA = new GeneradorPDF();

try{

generadorPDFA.setArchivo(archivo);

generadorPDFA.generadorPDF();

}catch(MyExcepcionA | NullPointerException e1){

err.println("Ha ocurrido una excepcion de tipo:"+e1.getCause());

e1.printStackTrace();

//Salida:

//GeneradorPDF

//Ha ocurrido una excepcion de tipo:null

//com.codemonkey.MyExcepcionA: El archivo no puede ser null

// at com.codemonkey.GeneradorPDF.generadorPDF(Main.java:47)

// at com.codemonkey.Main.main(Main.java:16)

class GeneradorPDF {
private String archivo;

GeneradorPDF(){

out.println("GeneradorPDF");

GeneradorPDF(String archivo){

super();

this.archivo=archivo;

public String getArchivo(){

return archivo;

public void setArchivo(String archivo){

this.archivo=archivo;

public void generadorPDF() throws MyExcepcionA{

if(this.archivo == null){

throw new MyExcepcionA("El archivo no puede ser null");

class MyExcepcionA extends Exception{

public MyExcepcionA(){
super();

public MyExcepcionA(String msg){

super(msg);

*/

//2

/*

import static java.lang.System.out;

public class Main{

static int c, a=10, b=21/2; //10

static{

c=a;

//Correcto:

static public void main(String $[]){

out.println(b);

//Correcto:

//static public void main(String... $){

// out.println(b);

//}
//Correcto:

//public static void main(String ... args){

// out.println(b);

//}

//Correcto:

//public static void main(String[] args){

// out.println(b);

//}

}*/

//Salida:

//10

//1

/*

import static java.lang.System.out;

public class Main{

public static void main(String[] args){

FinalClass finalClass = new FinalClass();

finalClass.metodoFinal();//Hola, desde FinalClass

XClass xClass = new XClass();

xClass.saludar();//Hola, desde XClass

Object pClass = new PClass();//Hola, desde PClass


QClass qClass = new QClass();//Hola, desde PClass

//RClass rClass = new RClass();//No compilará, se crea un ciclo

final class FinalClass {

final void metodoFinal(){

out.println("Hola, desde FinalClass");

abstract class AbstractClass {

abstract void saludar();

class XClass extends AbstractClass {

@Override

public void saludar(){

out.println("Hola, desde XClass");

class PClass extends Object{

PClass(){

this("Hola, desde PClass");//Invoca al constructor PClass(String msg)

PClass(String msg){
out.println(msg);

class QClass extends PClass{}

//Error: se creara un ciclo

class RClass extends RClass{

protected RClass(){

super();//Invocara al constructor PClass()

this("Hola, desde RClass");//Invoca al constructor RClass(String msg)

RClass(String msg){

out.println(msg);

*/