Results 1 to 8 of 8
  1. #1
    bytescode is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default What wrong with it?case : public static void main

    Hi all,

    Please help me.

    This is about inheretance in Java SE, at Ubuntu 9.04 using Eclipse.

    My script like this (file : Toko.java)
    Java Code:
    //kelas TokoBuku
    class TokoBuku {
        int IDToko;
        String NamaToko;
        String AlamatToko;
    
        //konstructor
        public TokoBuku(int ID,String Nama,String Alamat) {
        IDToko=ID;
        NamaToko = Nama;
        AlamatToko = Alamat;
    }
    
    //methode tampilkan    
    public void DisplayDetails () {
    	System.out.println("ID 			 = " + IDToko);
    	System.out.println("Nama Toko 	 = " + NamaToko);
    	System.out.println("Alamat            = " + AlamatToko);
    }    
    
    //kelas Buku
    class Buku {
    	int IDBuku;
    	String JudulBuku;
    	float HargaBuku;
    
    	//konstructor
    	public Buku (int ID, String judul, float harga) {
    		IDBuku = ID;
    		JudulBuku = judul;
    		HargaBuku = harga;
    	}
    
    	//methode tampilkan
    	public void DisplayDetails () {
    	    System.out.println("ID 		  = " + IDBuku);
    	    System.out.println("Judul Buku     = " + JudulBuku);
    	    System.out.println("Harga Buku    = " + HargaBuku);
    	}
    
    }
    
    //Kelas Komik 
    //diturunkan dari kelas Buku
    class Komik extends Buku {
        String KategoriKomik;
        String JudulKomik;
    
        //konstruktor
        public Komik (int bID, String judul, float harga, String kategorikomik, String judulkomik) {
        super(bID, judul, harga);
        KategoriKomik = kategorikomik;
        JudulKomik = judulkomik;
    }
    
        //methode tampilkan
        public void DisplayDetails () {
        	super.DisplayDetails();
        	System.out.println("Kategori 	= " + KategoriKomik);
        	System.out.println("Judul  	= " + JudulKomik);
    }    
    }
    
    //kelas utama
    //ada main
    public class Toko {
    	public static void main(String[] args) {
    		Komik komik = new Komik(111, "Shinchan", 20000, "anime", "anak - anak");
    		Buku buku = new Buku(222, "Ramayana", 25000);
    		TokoBuku tokobuku = new TokoBuku(333, "Buku Bacaan", "Jl Asoka 12 Denpasar");
    		komik.DisplayDetails();
    		buku.DisplayDetails();
    		tokobuku.DisplayDetails();
    	}
    }
    }
    When I compile and run it, Eclipse show error message like this :
    The methode main cannot be declared static, static methode can be declared in a static or top level type

    But when I remove static, it can't run too.

    So what wrong with this? Please help me. Thanks.
    Last edited by bytescode; 02-10-2011 at 04:32 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    It seems you have the Toku class inside the TokuBuku class. Is this what you want? To make your code and life easier you should put each class in its own file. Unless it really is an inner class.

  3. #3
    bytescode is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Maybe like that. But in this case I try to make a simple code using Java to implement inheretance. Toko mean store in Indonesian language.

    OK I will try your suggest :)

    Thanks bro.

  4. #4
    bytescode is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Done :D

    I create package named anak inside project, and move class TokoBuku inside it. Now it run well :)

    Thanks for your help :D

  5. #5
    castiel is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default Inner classes can't have static methods

    Here, i fixed it up for you, read my comments


    Java Code:
    //kelas TokoBuku
    class TokoBuku {
    	int IDToko;
    	String NamaToko;
    	String AlamatToko;
    
    	//konstructor
    	public TokoBuku(int ID,String Nama,String Alamat) {
    		IDToko=ID;
    		NamaToko = Nama;
    		AlamatToko = Alamat;
    	}
    
    	//methode tampilkan    
    	public void DisplayDetails () {
    		System.out.println("ID 			 = " + IDToko);
    		System.out.println("Nama Toko 	 = " + NamaToko);
    		System.out.println("Alamat            = " + AlamatToko);
    	}    
    
    	//kelas Buku
    	class Buku {
    		int IDBuku;
    		String JudulBuku;
    		float HargaBuku;
    
    		//konstructor
    		public Buku (int ID, String judul, float harga) {
    			IDBuku = ID;
    			JudulBuku = judul;
    			HargaBuku = harga;
    		}
    
    		//methode tampilkan
    		public void DisplayDetails () {
    			System.out.println("ID 		  = " + IDBuku);
    			System.out.println("Judul Buku     = " + JudulBuku);
    			System.out.println("Harga Buku    = " + HargaBuku);
    		}
    
    	}
    
    	//Kelas Komik 
    	//diturunkan dari kelas Buku
    	class Komik extends Buku {
    		String KategoriKomik;
    		String JudulKomik;
    
    		//konstruktor
    		public Komik (int bID, String judul, float harga, String kategorikomik, String judulkomik) {
    			super(bID, judul, harga);
    			KategoriKomik = kategorikomik;
    			JudulKomik = judulkomik;
    		}
    
    		//methode tampilkan
    		public void DisplayDetails () {
    			super.DisplayDetails();
    			System.out.println("Kategori 	= " + KategoriKomik);
    			System.out.println("Judul  	= " + JudulKomik);
    		}    
    	}
    
    	//kelas utama
    	//ada main
    	//public class Toko {     //I got rid of this because you can't have inner classes with static methods
    		public static void main(String[] args) {
    			
    			//As you have created inner classes insdie TokoBuku
    			//you must first create a TokoBuko object before you can 
    			//create the inner class objects
    			TokoBuku tok = new TokoBuku(3, "john", "smith");
    				
    			Komik komik = tok.new Komik(111, "Shinchan", 20000, "anime", "anak - anak");
    			Buku buku = tok.new Buku(222, "Ramayana", 25000);
    			TokoBuku tokobuku = new TokoBuku(333, "Buku Bacaan", "Jl Asoka 12 Denpasar");
    			komik.DisplayDetails();
    			buku.DisplayDetails();
    			tokobuku.DisplayDetails();
    		}
    //	}
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by castiel View Post
    I got rid of this because you can't have inner classes with static methods
    Unless the class is declared as static as well.

  7. #7
    bytescode is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Right :D

    Thanks for your help :D

  8. #8
    bytescode is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    This my fix code :D

    File : Toko.java

    Java Code:
    //kelas TokoBuku
    class TokoBuku {
        int IDToko;
        String NamaToko;
        String AlamatToko;
    
        //konstructor
        public TokoBuku(int ID,String Nama,String Alamat) {
        IDToko=ID;
        NamaToko = Nama;
        AlamatToko = Alamat;
    }
    
    //methode tampilkan    
    public void DisplayDetails () {
    	System.out.println("ID 			 	= " + IDToko);
    	System.out.println("Nama Toko 	 	= " + NamaToko);
    	System.out.println("Alamat       	        = " + AlamatToko);
    }   
    }
    
    
    //kelas Buku
    class Buku {
    	int IDBuku;
    	String JudulBuku;
    	float HargaBuku;
    
    	//konstructor
    	public Buku (int ID, String judul, float harga) {
    		IDBuku = ID;
    		JudulBuku = judul;
    		HargaBuku = harga;
    	}
    
    	//methode tampilkan
    	public void DisplayDetails () {
    	    System.out.println("ID 		  	= " + IDBuku);
    	    System.out.println("Judul Buku     	= " + JudulBuku);
    	    System.out.println("Harga Buku    	= " + HargaBuku);
    	}
    
    }
    
    //Kelas Komik 
    //diturunkan dari kelas Buku
    class Komik extends Buku {
        String KategoriKomik;
        String JudulKomik;
    
        //konstruktor
        public Komik (int bID, String judul, float harga, String kategorikomik, String judulkomik) {
        super(bID, judul, harga);
        KategoriKomik = kategorikomik;
        JudulKomik = judulkomik;
    }
    
        //methode tampilkan
        public void DisplayDetails () {
        	super.DisplayDetails();
        	System.out.println("Kategori   = " + KategoriKomik);
        	System.out.println("Judul  	 = " + JudulKomik);
    }    
    }
    
    //kelas utama
    //ada main
    public class TokoBuku {
    	public static void main(String[] args) {
    		Komik komik = new Komik(111, "Shinchan", 20000, "anime", "anak - anak");
    		Buku buku = new Buku(222, "Ramayana", 25000);
    		TokoBuku tokobuku = new TokoBuku(333, "Buku Bacaan", "Jl Asoka 12 Denpasar");
    		komik.DisplayDetails();
    		buku.DisplayDetails();
    		toko.DisplayDetails();
    	}
    }

    And this is it output :
    ID = 111
    Judul Buku = Shinchan
    Harga Buku = 20000.0
    Kategori = anime
    Judul = anak - anak
    ID = 222
    Judul Buku = Ramayana
    Harga Buku = 25000.0
    ID = 333
    Nama Toko = Buku Bacaan
    Alamat = Jl Asoka 12 Denpasar

Similar Threads

  1. Replies: 1
    Last Post: 12-04-2010, 05:41 PM
  2. Public static void main (String args[])
    By arefeh in forum New To Java
    Replies: 12
    Last Post: 01-28-2010, 11:58 AM
  3. Replies: 9
    Last Post: 11-22-2008, 11:03 PM
  4. Replies: 5
    Last Post: 10-06-2008, 12:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •