Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to call a variable from a different class so I can display and update the variable. I do not know how to properly call the variable and use it, eclipse throws up an error. Help would be much appreciated.

Thank you

What I have tried:

I have made the variable and gave it a value in a class called TicketHandler
public int TicketCost, AmountOfTickets;

public TicketHandler (int TicketCost, int AmountOfTickets){
		
		this.TicketCost = TicketCost;
		this.AmountOfTickets = AmountOfTickets;
			
	}
	public TicketHandler() {
			
		TicketCost = 200;
		AmountOfTickets = 50;

I have made the getter for the variable in the same class
public int getAmountOfTickets() {
		
		return AmountOfTickets;


I have tried calling it in a different class by using this line
getAmountOfTickts = AmuontOfTickets - 1;



TicketHandler Red = new TicketHandler (200,50);
		.
TicketHandler Green = new TicketHandler (200,50);
		
TicketHandler Blue = new TicketHandler (200,50);
		
TicketHandler Yellow = new TicketHandler (200,50);

These are some constructors that use the AmountOfTickets variable (the number 50)
Posted
Updated 28-Mar-17 21:55pm
Comments
[no name] 28-Mar-17 18:52pm    
You need to figure out what it is that you are trying to do. First you do not call variables. It's really helpful to tell us the errors that you get as we can't see your screen from here. getAmountOfTickets only returns a value so trying to assign a value to it is never going to work.

1 solution

Your code is confusing and you are making your variables public; they should be private, so you access them via the getter/setter. You are also trying to access them from outside the class, but without any object reference. I would suggest you go to The Java™ Tutorials[^] and learn about classes and how to use them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900