RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4908 | Replies: 2
Reply
Join Date: Jul 2005
Posts: 1
Reputation: JavaDeveloper is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
JavaDeveloper JavaDeveloper is offline Offline
Newbie Poster

how to pass non-static variable to main method

  #1  
Jul 16th, 2005
How is it to pass "int [] rank" to main method?

public class Card
{
private int [] rank= new int [13];
private String [] suit={"Spades","Hearts","Diamonds","Clubs"};
private static void rankMaker (int []rank)
{
for (int i=1;i<=13;i++)
rank[i]=i;
}
public static void main (String []args)
{
System.out.println("Jack ="+rank[11]);
}
}

Ignoring the way I am making this deck, what I want to know is how generally can I use "int [] rank" (or any other variable declared outside the main) in other methods?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: how to pass non-static variable to main method

  #2  
Jul 17th, 2005
You can't, period.

The normal way is to have the main method do nothing except handle commandline arguments, create a single instance of the application (usually its own class), and start that instance.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: how to pass non-static variable to main method

  #3  
Jul 17th, 2005
Just in case, here's kindof what he's talking about:

public class Card
{
	private int [] rank= new int [13];
	private String [] suit={"Spades","Hearts","Diamonds","Clubs"}; //Not used?


	public Card()
	{
		super();
	}
	


	public void setRanks(int []rank)
	{
		for (int i=1;i<=13;i++)
		{
			rank[i]=i;
		}
	}


	public int getRankAt(int index)
	{
		return this.rank[index];
	}
}


class TestCard
{
	public static void main(String[] args)
	{
		Card c = new Card();
		System.out.println(c.getRankAt(11) + "");
	}
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:53 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC