• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
illegal start of expression java
by (1.6k points)  
reshown by

1 Answer

0 votes

Fix  for "illegal start of expression" error in Java:

In error called illegal start of expression there's two types of
errors :
1)syntax errors things that are not correct and then we
have a lot of exceptions which gets
thrown at at runtime this one happens to
be a syntax type error and it's very
common especially when you're starting
out in programming so in this example I
have my main method and I thought well
you know the first thing I want to do is
I'll create a method to get my user data
or return some kind of string like maybe
their name or something and I typed it
and I'm looking at it going wow that's
that's the exact pattern I'm supposed to
make for a method right it's got an
access identifier public private or
protected you know this case it's static
it returns a string I I've got a name I
got brackets it should be getting but I
get this error even legal start of
expression and what that's telling us is
actually here we have the main method
and you can see highlighted in yellow
you can see the start of the main method
and then the end of the main method then
inside of it I have my new method this
get user data method and you can't have
it that way
methods have to exist unto themselves
well thanks for here so this method
starts and stops and then a new method
starts and stops and then the other area
that we're seeing here is has missing
return statement now what that means is
here when I declared the method header
it has a return type in this case string
to be int or double or student or any
type of object but it's telling the
system that I'm going to return
something to whatever method calls it
because there's nothing here it's as I'm
missing a return statement so I'll have
to do is return something it's been a
piece and say return yeah and then my
air is going to go away so the two
items here illegal started expression is
usually a bracketing issues to just make
sure you look at the start and finish of
each method and then the other one we
looked at is missing return statement
which means if you say you're going to
return something in your code you have
to get to a point where you can return
things another scenario that people get
confused at is it says like if you know
five is less than four from yeah in this
case it may look like we've got a return
statement but we still get this error
and the reason is this condition in the
if statement we're going to have some
kind of conditional statement here so
it's quite possible that we never get to
this line of code so it is in this
scenario you'd have to rate in else and
then return I hate to whenever you say
you return something your code at some
point has to get to a line of code that
says return and otherwise you will see
that a missing return statement error
by (9.7k points)