Jump to content

compilation error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
eman ahmed

eman ahmed

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
I wrote block of code and has a compilation error in it
please help me to found the reason of that error

public class AllStudent {

 LinkedList students =new LinkedList();

public void addNewStudents(Student s){

// there is any compilation error in the nest line

	students.add(s);

	

}

public static void main(String[] args) {

//compilation error in the next line and the compiler said to me

// can't make a static refrence to non static method 

	students.add("fgk");

	

}

}

I know that I should write static before Linkedlist but why?

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
You are trying to access non-static content from a static content, which cannot be done without a reference.

#3
Generic

Generic

    Newbie

  • Members
  • PipPip
  • 26 posts
public class AllStudent {

        private static LinkedList students = new LinkedList();

        public static void addNewStudents(Student s) {
// there is any compilation error in the nest line
            students.add(s);
        }

        public static void main(String[] args) {
//compilation error in the next line and the compiler said to me
// can't make a static refrence to non static method 
            students.add("fgk");

        }
    }
Your variables and function have to be static unless you make a new object of AllStudent to do addNewStudent

#4
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Alternatively, could you not make an instance of AllStudent before invoking the add() method? e.g.


public class AllStudent {


LinkedList students =new LinkedList();


public void addNewStudents(Student s){

	students.add(s);	

}        //end addNewStudent


public static void main(String[] args) {

	AllStudent example = new AllStudent();	//create a new AllStudent object called 'example'

	example.addNewStudent("fgk");          //use example's addNewStudent method to add a new student 'fgk'.

}	//end main


}      //end class


Then, you're creating an instance of the class from Main, and using that instance to access the nonstatic content as opposed to making everything static. addNewStudent(Student) invoked the add() method for the linked list anyways, so you can create the class instance from Main(String[]), and then use the addNewStudent method to add a new student instead of trying to add it directly.
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)

#5
vachovsky

vachovsky

    Newbie

  • Members
  • Pip
  • 8 posts
hm... LinkedList is generic! no?


LinkedList< String > students = new LinkedList< String > ();



#6
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts

vachovsky said:

hm... LinkedList is generic! no?

LinkedList< String > students = new LinkedList< String > ();

OMG... I wish I'd have known you could do that a couple of hours ago... Would have saved me a massive headache! ^^
I was wondering how to have a Vector hold data of type JButton, instead of Object, as it was causing compilation problems... well, now I know ^_^ Thanks, Vachovsky!
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)