Results 1 to 3 of 3

Thread: constructors

  1. #1
    Join Date
    Dec 2007
    Posts
    24
    Rep Power
    0

    Default constructors

    class student
    {
    String name;
    String dep;
    int no;

    public student(String fn,String de,int n)
    {
    name=fn;
    dep=de;
    no=n;
    display();
    }
    public student()
    {

    }
    public void changedep(String dep)
    {
    this.dep=dep;
    System.out.println("dep of student is changed to "+dep);
    display();
    }
    public void display()
    {
    System.out.println("Name: "+name);
    System.out.println("Department: "+dep);
    System.out.println("Number: "+no);
    }
    }

    public class cla extends student
    {
    int nostu=0;
    public void joinstu(String n1,String d1,int no1)
    {
    student st=new student(n1,d1,no1);
    nostu++;
    System.out.print("The new Student "+n1);System.out.print(" with no"+no1);System.out.print("is addeed to"+d1);System.out.print("departmenet in the class");
    }
    public void nostud()
    {
    System.out.println("No of students is "+nostu);
    }
    public static void main(String args[])
    {
    joinstu("kami","ece",39);
    nostud();
    changedep("cse");
    }
    }







    its my program iam coming errors are




    D:\java>javac cla.java
    cla.java:47: non-static method joinstu(java.lang.String,java.lang.String,int) ca
    nnot be referenced from a static context
    joinstu("kami","ece",39);
    ^
    cla.java:48: non-static method nostud() cannot be referenced from a static conte
    xt
    nostud();
    ^
    cla.java:49: non-static method changedep(java.lang.String) cannot be referenced
    from a static context
    changedep("cse");
    ^
    3 errors

    D:\java>



    then what i hav 2 do pls advice

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class StudExtension extends Student
    {
        int nostu=0;
    
        public void joinstu(String n1,String d1,int no1)
        { 
            Student st=new Student(n1,d1,no1);
            nostu++;
            System.out.print("The new Student "+n1);
            System.out.print(" with no"+no1);
            System.out.print("is addeed to"+d1);
            System.out.print("departmenet in the class");
        }
     
        public void nostud()
        {
            System.out.println("No of students is "+nostu);
        }
    
        public static void main(String[] args)
        {
            StudExtension se = new StudExtension();
            se.joinstu("kami","ece",39);
            se.nostud();
            se.changedep("cse"); 
        }
    }
    
    class Student
    {
        ...
    
        public Student()
        {
            this(null, null, 0);
        }

  3. #3
    Join Date
    Dec 2007
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 08:59 AM
  2. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •