4 Replies - 647 Views - Last Post: 08 February 2015 - 12:57 PM Rate Topic: -----

#1 Soumikbhat   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 01-September 13

beginner java question

Posted 08 February 2015 - 12:08 PM

I've just started out on java programming and am stuck on this :

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public class A
    {
        int a;  char b;
        public A(int x, char y )
        {
	    a=x; b=y;
	}

        public void print()
        {
            System.out.println("Print function called");
        }

        public A()
        {
        }
    }

    public interface B
    {
	void unfriend();
    }	
    public class C extends A implements B
    {
	char d;
	public  C(int x, char y, char z)
        {
	    super(x,y);
            d=z;
	}

	@Override
	public void unfriend()
        {
	    System.out.println("unfriended");
	}
    }

    public static void main (String[] args) throws java.lang.Exception
    {	
        C s = new C(); //error line

	s.unfriend();
	// your code goes here
    }
}



The line marked as error line gives
error: non-static variable this cannot be referenced from a static context C s =new C();

I don't have much clue about this problem, so kindly throw some light on this and its workaround...

This post has been edited by xclite: 08 February 2015 - 12:31 PM
Reason for edit:: Made formatting consistent


Is This A Good Question/Topic? 1
  • +

Replies To: beginner java question

#2 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: beginner java question

Posted 08 February 2015 - 12:28 PM

Since the class C is a nested member of your class Ideone, it can't be referenced from a static context - your static context being the main method.

Make C static and it should stop giving you that error. For more information, see: http://docs.oracle.c...aOO/nested.html

Also, you'll notice after that that you aren't sending the correct parameters to C's constructor - you didn't get C a constructor which takes no arguments, yet you're calling it like you did.
Was This Post Helpful? 1
  • +
  • -

#3 Soumikbhat   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 87
  • Joined: 01-September 13

Re: beginner java question

Posted 08 February 2015 - 12:43 PM

View Postxclite, on 08 February 2015 - 12:28 PM, said:

Since the class C is a nested member of your class Ideone, it can't be referenced from a static context - your static context being the main method.

Make C static and it should stop giving you that error. For more information, see: http://docs.oracle.c...aOO/nested.html

Also, you'll notice after that that you aren't sending the correct parameters to C's constructor - you didn't get C a constructor which takes no arguments, yet you're calling it like you did.

thank you for that...

I changed my code to
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{	public class A{
 int a;  char b;
     public A(int x, char y ){
	
	a=x; b=y;
		        }
        

      public void print() 
                        {
            System.out.println("Print function called")	;
                        }
         
      public A(){
	
                }

 }


public interface B{
	

           void unfriend();
}	


static public class C extends A implements B{
	char d;
	public  C(int x, char y, char z){
		super(x,y); //error line
		//this->d=z;
			d=z;
	}
	public C(){
		
	}
	@Override
	public void unfriend(){
		System.out.println("unfriended");
	}
	
}

	public static void main (String[] args) throws java.lang.Exception
	{	C s=new C(2,'a','b'); 
		
		
		s.unfriend();
		// your code goes here
	}
}


Now super(x,y) throws an error :
error: no enclosing instance of type Ideone is in scope
super(x,y);
The code can be viewed at http://ideone.com/oC8ent
Thanks...

This post has been edited by Soumikbhat: 08 February 2015 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: beginner java question

Posted 08 February 2015 - 12:52 PM

Ugh you are making this harder and harder for yourself
Working with inner classes and static inner classes as a beginner is not a good idea at all

Make your life easy:
- Remove the static keyword again
- Move every class and interface out into its own file
This way you won't be having any inner classes, and the compiler will be happy :)

Read about inner classes when you're ready for it, but trust me, that shouldn't be your focus for quite some time :)
Was This Post Helpful? 2
  • +
  • -

#5 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: beginner java question

Posted 08 February 2015 - 12:57 PM

Good point - the reason I didn't suggest that was because they're working in Ideone, but inner classes are more rarely used than 1 class per file anyway.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1