# Solved: "Illegal start of an expression" error in Java



## tehskanksta12 (Feb 5, 2011)

Alright so i am using BlueJ and i am very VERY new to programming. I am programming in Java and i am getting a compile error stating "Illegal start of an expression" on line 5 where it says "private String firstName;" Thank you in advance for your help!!

public class PlayerClass
{
public static void main (String[] args)
{
private String firstName;
private String lastName;
private int idNumber;
private int chips;
}
public Player()
{
firstName="";
lastName="";
idNumber=0;
chips=0;
}
public Player(String n1, String ln, int idnum, int ch1)
{
firstName=n1;
lastName=ln;
idNumber=idnum;
chips=ch1;
}
}


----------



## andythepandy (Jul 2, 2006)

The Illegal start of expression error is being generated because variables declared in the main method cannot have what are called access modifiers(public, private or protected). Any variables declared in the main method can't be accessed by your class methods, Player() and Player(String n1....) so your two methods will through errors as they can't 'see' the variables you declare in main.

Anything you want accessible by class methods should be defined in the class, like this:


```
public class PlayerClass
{
	private String firstName;
	private String lastName;
	private int idNumber;
	private int chips;
	
	public static void main (String[] args)
	{
		
	}
	public void Player()
	{
		firstName="";
		lastName="";
		idNumber=0;
		chips=0;
	}
	public void Player(String n1, String ln, int idnum, int ch1)
	{
		firstName=n1;
		lastName=ln;
		idNumber=idnum;
		chips=ch1;
	}
}
```
You may also notice that I added return types to your methods. These are needed to show what the method returns, for example if this was a method:

```
[COLOR="Red"]int [/COLOR]add(int num1, int num2) 
{
	int sum = num1 + num2;
	[COLOR="Red"]return sum;[/COLOR]
}
```
you can see that this method returns the _int _sum. You need to tell the compiler what you will be returning by adding the type in the method (in red)
As your methods don't return anything, you give them a return type of _void_

Other stuff that doesn't generate errors:

Having an empty main method as the code above does will mean absolutely nothing happens when the program is run. To create a new instance of you class, you'll want this:

```
public static void main (String[] args)
{
	PlayerClass playerclass = new PlayerClass();
}
```
Also, you class should have a constructor. This method is automatically called when you create a new instance of the class like this:

```
PlayerClass pc = new PlayerClass();
```
To make a constructor, you define a method with no return type and is public. The constructor is where initialisation of variables should take place. With your code, your constructor(s) might look like this: (I have just used your two existing methods and renamed them as all they did was initialisation)

```
public PlayerClass()
{
	firstName="";
	lastName="";
	idNumber=0;
	chips=0;
}
public PlayerClass(String n1, String ln, int idnum, int ch1)
{
	firstName=n1;
	lastName=ln;
	idNumber=idnum;
	chips=ch1;
}
```
I realise that some of that may sound complicated, but as you become more used to programming, you will understand it.
I suggest if you aren't already following a good beginners tutorial (liek http://www.thenewboston.com/?cat=36&pOpen=tutorial) as explaining how to use classes properly and how to use the main method would make for a rather large post 

Andy


----------



## tehskanksta12 (Feb 5, 2011)

Thank you SOO much. This helped me out incredibly


----------

