Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Problem with calling objects from same-class methods in main

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with calling objects from same-class methods in main

    In my main class, I have a method that creates a list of Fossil objects. I put it into a separate method to make the overall code cleaner. In main, I am able to call the method createFossils() without errors but as soon as I try to use the Fossil objects themselves (ex. Museum.addFossil( amber.getName() ); ) I get the "cannot find symbol" error for the "amber" object, which I initialized in createFossils().
    I know I could get around this by initializing the Fossil objects inside main, but it would be just a big chuck of 'Fossil ____ = new Fossil' lines. Is there any way to make this work? I was thinking it was the method types of createFossils(), which right now I have public static void createFossils(). Does it need to be something more like private void createFossils() or something similar? Because in that case I wouldn't be able to call it in main, right? Any input would be appreciated.

    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,618
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Problem with calling objects from same-class methods in main

    cannot find symbol"
    Are the definitions for the symbols/variables that can't be found in scope where you are trying to use them?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calling objects from same-class methods in main

    Quote Originally Posted by Norm View Post
    Are the definitions for the symbols/variables that can't be found in scope where you are trying to use them?
    If by definitions for the symbols/variables you mean the initializing line (Fossil amber = new Fossil("Amber", 1200, false) being the same as the constructor in the Fossil class, then yes. I'm trying to test adding a Fossil to a Museum object, but the main class does not recognize the fossils being initialized using the createFossils() method
    If that's not what you meant, then I don't understand, sorry.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,618
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Problem with calling objects from same-class methods in main

    Sample definition for the symbols/variables
     Fossil amber;  //  define a variable named amber of type Fossil

    Is the definition of amber in scope (within same pair of {}s) as where it is being used?

    the main class does not recognize the fossils being initialized using the createFossils() method
    Please post the full text of the error message and the code that is causing the error.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calling objects from same-class methods in main

    Ah, okay I see. No, not directly.
    Here's how I have it set up:

    (main)
    Museum muse = new Museum();

    createFossils(); //<-- This is where amber is being defined. Unless this would be considered in scope, then no.
    //createFossils() is another method inside the same class as main

    muse.addFossil(amber.getName());

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,618
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Problem with calling objects from same-class methods in main

    Sorry, you few lines of code don't give anyone enough information to know what you are doing or what the problem is.

    If amber is defined inside of a method then it will NOT be known anywhere else in the program except inside that method.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calling objects from same-class methods in main

    Quote Originally Posted by Norm View Post
    If amber is defined inside of a method then it will NOT be known anywhere else in the program except inside that method.
    Okay that clears a lot up for me. Like I said, I know I can create each and every Fossil object in main but do you know of a way to create all 25 of them that I need in one line of code? That was my overall goal of using the createFossils() method. If not, then I can just do it one at a time.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,618
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Problem with calling objects from same-class methods in main

    a way to create all 25 of them that I need in one line of code?
    I don't know how to create 25 objects in one statement.
    It can be done with 2 statements: a for loop and a new statement
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calling objects from same-class methods in main

    Ah well, it was worth a shot. Thanks for the help though

  10. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Problem with calling objects from same-class methods in main

    In your main program make a datastructure of amber objects (e.g ArrayList) en provide this with amber objects (with your createFossils methode) then with a iterator object you can use your addFossil() methode to get the amber objects. I don't think it's a good idea to get the amber objects with their name (.getname())

  11. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calling objects from same-class methods in main

    Quote Originally Posted by alaindhaene View Post
    I don't think it's a good idea to get the amber objects with their name (.getname())
    Amber is just one of the 25 Fossil objects I need to make. (stego tail, stego torso, stego skull, trex tail, etc). In my Museum class, the addFossil method needs a string that adds to the array of Fossils in the Museum, hence the getName(). But I see what you're saying with using the array, then changing addFossils() to pick from the array.[COLOR="Silver"]

  12. #12
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calling objects from same-class methods in main

    Please provide your code...

  13. #13
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with calling objects from same-class methods in main

    import java.util.*;
    import java.io.*;
    import java.lang.*;
    public class AnimalCrossingTest
    {
    	public static String name, town, temp;
    	public static Scanner scan = new Scanner(System.in);
    	public static int bellBalance;
     
    	public static void intro()
    	{
    		//*****INTRO*****
    		System.out.println("Welcome to the crazy, wonderful\nworld of Animal Crossing!");
    		System.out.println();
    		System.out.println("*******************************");
    		System.out.println();
     
    		//**NAME**
    		boolean ready=false;
    		do {
    			System.out.print("Go ahead and enter your name: ");
    			name = scan.nextLine();
    			char n = Character.toUpperCase(name.charAt(0));
    			String qu = name.substring(1);
    			name = n+qu;
    			System.out.println();
    			System.out.print("Okay, "+name+"!\n...that is your name, right? ");
    			temp = scan.nextLine();
    			System.out.println();
    			if(temp.equalsIgnoreCase("yes"))
    			{
    				System.out.println("Whew! I'm glad that's settled, "+name+"!");
    				System.out.println();
    				ready=true;
    			}
    		} while(ready==false);	
     
    		//**GENDER**
    		ready=false;
    		boolean boy=true;
    		do {
    			do {
    				System.out.print("Well, then are you a boy or a girl? ");
    				temp=scan.nextLine();
    				if(temp.equalsIgnoreCase("girl"))
    				{
    					boy=false;
    					System.out.println();
    					ready=true;
    				}
    				if(temp.equalsIgnoreCase("boy"))
    				{
    					boy=true;
    					System.out.println();
    					ready=true;
    				}
    			} while(ready==false);
    			if(boy==true)
    			{
    				System.out.print("You're a boy, huh? ");
    				temp = scan.nextLine();
    				if(temp.equalsIgnoreCase("no"))
    				{
    					ready=false;
    				}
    				System.out.println();
    			}
    			if(boy==false)
    			{
    				System.out.print("You're a girl, huh? ");
    				temp = scan.nextLine();
    				if(temp.equalsIgnoreCase("no"))
    				{
    					ready=false;
    				}
    				System.out.println();
    			}	
    		} while(!temp.equalsIgnoreCase("yes"));
     
    		//**TOWN*NAME**
    		ready=false;
    		do {
    			do {
    				System.out.print("So, where you headed? ");
    				town = scan.nextLine();
    				char t = Character.toUpperCase(town.charAt(0));
    				String q = town.substring(1);
    				town = t+q;
    				System.out.println();
    				System.out.print("Did I hear that right? I think you said "+town+"... ");
    				temp=scan.nextLine();
    			} while(!temp.equalsIgnoreCase("yes"));
    			if(temp.equalsIgnoreCase("yes"))
    			{
    				System.out.println();
    				System.out.println("Onward to "+town+"!");
    				ready=true;
    			}
    		} while(ready=false);
     
    		//**CREATE*PLAYER**
    		System.out.println();
    		CharacterObj player = new CharacterObj(name,town,boy,1000);
    		System.out.println(player.toString());
     
    		//**CREATING*MUSEUM**
    		Museum muse = new Museum();
    		//**ADDING*FOSSILS*TO*MUSEUM**
    		createFossils();
    		muse.addFossil(amber);
    		System.out.println("Current collection of the museum in "+town+":");
    		System.out.println();
    		System.out.println(muse.toString());
    		muse.listFossils();
     
    		//**CREATING*THE*TOWN**
    		Town myTown = new Town(town, 0);
    		myTown.showMap();
    	}
     
    	//Name of fossil, price if sold, and owned or not
    	public static void createFossils()
    	{
    		Fossil amber = new Fossil("Amber", 1200, false);
    		Fossil dinoe = new Fossil("Dinosaur Egg", 1400, false);
    		Fossil ammo = new Fossil("Ammonite", 1100, false);
    		Fossil track = new Fossil("Dinosaur Track", 1000, false);
    		Fossil mamTo = new Fossil("Mammoth Torso", 2500, false);
    		Fossil mamSk = new Fossil("Mammoth Skull", 3000, false);
    		Fossil plesTo = new Fossil("Plesio Torso", 4500, false);
    		Fossil plesNe = new Fossil("Plesio Neck", 4500, false);
    		Fossil plesSk = new Fossil("Plesio Skull", 4000, false);
    		Fossil pteRW = new Fossil("Ptera Right Wing", 4500, false);
    		Fossil pteLW = new Fossil("Ptera Left Wing", 4500, false);
    		Fossil pteSk = new Fossil("Ptera Skull", 4000, false);
    		Fossil steTi = new Fossil("Stego Tail", 4000, false);
    		Fossil steTo = new Fossil("Stego Torso", 4500, false);
    		Fossil steSk = new Fossil("Stego Skull", 5000, false);
    		Fossil apaTa = new Fossil("Apato Tail", 4000, false);
    		Fossil apaTo = new Fossil("Apato Torso", 4500, false);
    		Fossil apaSk = new Fossil("Apato Skull", 5000, false);
    		Fossil rexTa = new Fossil("T-Rex Tail", 5000, false);
    		Fossil rexTo = new Fossil("T-Rex Torso", 5500, false);
    		Fossil rexSk = new Fossil("T-Rex Skull", 6000, false);
    		Fossil triTa = new Fossil("Tricera Tail", 4500, false);
    		Fossil triTo = new Fossil("Tricera Torso", 5000, false);
    		Fossil triSk = new Fossil("Tricera Skull", 5500, false);		
    	}
     
    	public static void createFish()
    	{
    		//River or big pond
    		Fish angel = new Fish("Angelfish", 3000, false);
    		Fish arapa = new Fish("Arapaima", 10000, false);
    		Fish arowa = new Fish("Arowana", 10000, false);
    		Fish barbe = new Fish("Barbel Steed", 200, false);
    		Fish bass = new Fish("Bass", 300, false);
    		Fish bitte = new Fish("Bitterling", 1300, false);
    		Fish blueg = new Fish("Bluegill", 120, false);
    		Fish carp = new Fish("Carp", 300, false);
    		Fish catfi = new Fish("Catfish", 200, false);
    		Fish cherr = new Fish("Cherry Salmon", 1300, false);
    		Fish cruci = new Fish("Crucian Carp", 120, false);
    		Fish dace = new Fish("Duce", 200, false);
    		Fish eel = new Fish("Eel", 2000, false);
    		Fish guppy = new Fish("Freshwater Guppy", 1300, false);
    		Fish killi = new Fish("Killifish", 300, false);
    		Fish koi = new Fish("Koi", 2000, false);
    		Fish lbass = new Fish("Large Bass", 3000, false);
    		Fish loach = new Fish("Loach", 300, false);
    		Fish palec = new Fish("Pale Chub", 200, false);
    		Fish piran = new Fish("Piranha", 6500, false);
    		Fish ponds = new Fish("Pond Smelt", 300, false);
    		Fish popey = new Fish("Popeyed Goldfish", 1300, false);
    		Fish rainb = new Fish("Rainbow Trout", 650, false);
    		Fish sbass = new Fish("Small Bass", 200, false);
    		Fish strin = new Fish("Stringfish", 15000, false);
    		Fish sweet = new Fish("Sweetfish", 1300, false);
    		//Big pond only
    		Fish brook = new Fish("Brook Trout", 150, false);
    		Fish gcatf = new Fish("Giant Catfish", 15000, false);
    		Fish gsnak = new Fish("Giant Snakehead", 6500, false);
    		//Ocean fish
    		Fish barre = new Fish("Barred Knifejaw", 5000, false);
    		Fish coela = new Fish("Coelacanth", 15000, false);
    		Fish jelly = new Fish("Jellyfish", 100, false);
    		Fish redsn = new Fish("Red Snapper", 3000, false);
    		Fish salmo = new Fish("Salmon", 650, false);
    		Fish seaba = new Fish("Sea Bass", 120, false);
    		//Small pond fish
    		Fish crawf = new Fish("Crawfish", 250, false);
    		Fish frog = new Fish("Frog", 250, false);
    		//Waterfall only
    		Fish lchar = new Fish("Large Char", 10000, false);
    	}
     
    	public static void createBugs()
    	{
    		//Flying about
    		Bug bande = new Bug("Banded Dragonfly", 4500, false);
    		Bug cbutt = new Bug("Common Butterfly", 80, false);
    		Bug cdrag = new Bug("Common Dragonfly", 130, false);
    		Bug darnd = new Bug("Darner Dragonfly", 200, false);
    		Bug mosqu = new Bug("Mosquito", 130, false);
    		Bug purpb = new Bug("Purple Butterfly", 2000, false);
    		Bug redbu = new Bug("Red Butterfly", 80, false);
    		Bug tigbu = new Bug("Tiger Butterfly", 200, false);
    		Bug yelbu = new Bug("Yellow Butterfly", 80, false);
    		//On trees
    		Bug bagwo = new Bug("Bagworm", 250, false);
    		Bug bee = new Bug("Bee", 4500, false);
    		Bug broci = new Bug("Brown Cicada", 200, false);
    		Bug drone = new Bug("Drone Beetle", 80, false);
    		Bug dynas = new Bug("Dynastid Beetle", 1350, false);
    		Bug eveni = new Bug("Evening Cicada", 850, false);
    		Bug fstag = new Bug("Flat Stag Beetle", 2000, false);
    		Bug gbeet = new Bug("Giant Beetle", 10000, false);
    		Bug jewel = new Bug("Jewel Beetle", 3000, false);
    		Bug longh = new Bug("Longhorn Beetle", 200, false);
    		Bug mount = new Bug("Mountain Beetle", 2000, false);
    		Bug robus = new Bug("Robust Cicada", 300, false);
    		Bug sawst = new Bug("Saw Stag Beetle", 2000, false);
    		Bug spide = new Bug("Spider", 300, false);
    		Bug walke = new Bug("Walker Cicada", 400, false);
    		//On flowers
    		Bug ladyb = new Bug("Ladybug", 130, false);
    		Bug manti = new Bug("Mantis", 430, false);
    		Bug snail = new Bug("Snail", 250, false);
    		Bug splad = new Bug("Spotted Ladybug", 200, false);
    		//Near water
    		Bug firef = new Bug("Firefly", 250, false);
    		Bug ponds = new Bug("Pondskater", 130, false);
    		//In patches of grass or on ground
    		Bug bellc = new Bug("Bell Cricket", 430, false);
    		Bug grass = new Bug("Grasshopper", 130, false);
    		Bug llocu = new Bug("Long Locust", 200, false);
    		Bug mlocu = new Bug("Migratory Locust", 1350, false);
    		Bug pinec = new Bug("Pine Cricket", 100, false);
    		//Miscellaneous
    		Bug ant = new Bug("Ant", 80, false);
    		Bug cockr = new Bug("Cockroach", 5, false);
    		Bug molec = new Bug("Mole Cricket", 200, false);
    		Bug pillb = new Bug("Pill Bug", 250, false);
    	}
     
    	public static void createPaintings()
    	{
    		Painting amaze = new Painting("Amazing Painting", false);
    		Painting basic = new Painting("Basic Painting", false);
    		Painting classi = new Painting("Classic Painting", false);
    		Painting commo = new Painting("Common Painting", false);
    		Painting daint = new Painting("Dainty Painting", false);
    		Painting famou = new Painting("Famous Painting", false);
    		Painting fine = new Painting("Fine Painting", false);
    		Painting flowe = new Painting("Flowery Painting", false);
    		Painting movin = new Painting("Moving Painting", false);
    		Painting perfe = new Painting("Perfect Painting", false);
    		Painting quain = new Painting("Quaint Painting", false);
    		Painting rare = new Painting("Rare Painting", false);
    		Painting scary = new Painting("Scary Painting", false);
    		Painting stran = new Painting("Strange Painting", false);
    		Painting worth = new Painting("Worthy Painting", false);
    	}
     
    	public static void main(String[] args)
    	{
    		createFossils();
    		intro();
    	}
    }
    public class Museum
    {
    	private int foscount, fiscount, bugcount, paicount;
    	private String fosname, fisname, bugname, painame;
    	private String[] foscollection = new String[25];
    	private String[] fiscollection = new String[40];
    	private String[] bugcollection = new String[40];
    	private String[] paicollection = new String[15];
    	private boolean have;
     
    	public Museum()
    	{
    		foscount=0;
    		fiscount=0;
    		bugcount=0;
    		paicount=0;
    	}
     
    	public void addFossil(Fossil f)
    	{
    		for(int i=0;i<foscollection.length-1;i++)
    		{
    			if(foscollection[i]==null)
    			{
    				foscollection[i] = f.getName();
    				break;
    			}
    		}
    		foscount++;
    	}
     
    	public void addFish(Fish f)
    	{
    		for(int i=0;i<fiscollection.length-1;i++)
    		{
    			if(fiscollection[i]==null)
    			{
    				fiscollection[i] = f.getName();
    				break;
    			}
    		}
    		fiscount++;
    	}
     
    	public void addBug(Bug b)
    	{
    		for(int i=0;i<bugcollection.length-1;i++)
    		{
    			if(bugcollection[i]==null)
    			{
    				bugcollection[i] = b.getName();
    				break;
    			}
    		}
    		bugcount++;
    	}
     
    	public void addPainting(Painting p)
    	{
    		for(int i=0;i<paicollection.length-1;i++)
    		{
    			if(paicollection[i]==null)
    			{
    				paicollection[i] = p.getName();
    				break;
    			}
    		}
    		paicount++;
    	}
     
    	public void listFossils()
    	{
    		System.out.println();
    		System.out.print("Fossils: ");
    		for(int i=0;i<foscollection.length-1;i++)
    		{
    			if(foscollection[i]==null)
    			System.out.print(".");
    			else
    			System.out.print(foscollection[i]+" ");
    		}
    	}
     
    	public void listFish()
    	{
    		System.out.println();
    		System.out.print("Fish: ");
    		for(int i=0;i<fiscollection.length-1;i++)
    		{
    			if(fiscollection[i]==null)
    			System.out.print(".");
    			else
    			System.out.print(fiscollection[i]+"- ");
    		}
    	}
     
    	public void listBugs()
    	{
    		System.out.println();
    		System.out.print("Bugs: ");
    		for(int i=0;i<bugcollection.length-1;i++)
    		{
    			if(bugcollection[i]==null)
    			System.out.print(".");
    			else
    			System.out.print(bugcollection[i]+"- ");
    		}
    	}
     
    	public void listPaintings()
    	{
    		System.out.println();
    		System.out.print("Paintings: ");
    		for(int i=0;i<paicollection.length-1;i++)
    		{
    			if(paicollection[i]==null)
    			System.out.print(".");
    			else
    			System.out.print(paicollection[i]+"- ");
    		}
    	}
     
    	public String toString()
    	{
    		String tempo = "Fossils:   "+foscount+
    					 "\nFish:	   "+fiscount+
    					 "\nBugs:	   "+bugcount+
    					 "\nPaintings: "+paicount;
    		return tempo;
    	}
    }
    public class Fossil
    {
    	private String name;
    	private int price;
    	private boolean have;
     
    	public Fossil(String n, int p, boolean h)
    	{
    		name=n;
    		price=p;
    		have=h;
    	}
     
    	public String getName()
    	{
    		return name;
    	}
     
    	public int getPrice()
    	{
    		return price;
    	}
     
    	public boolean doHave()
    	{
    		return have;
    	}
     
    	public void setHave(boolean h)
    	{
    		have=h;
    	}
    }

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 01:30 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 01:09 PM
  3. Replies: 5
    Last Post: October 18th, 2012, 01:43 PM
  4. calling objects in a differnt class
    By jack_nutt in forum Object Oriented Programming
    Replies: 12
    Last Post: July 8th, 2011, 01:57 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 04:24 PM

Tags for this Thread