I'm just trying to use Method_In_SubClass() method from SubClass Class but I'm getting these errors. I tried by changing Method_In_SubClass to Static but still getting errors
public class Sub {
//Method 1 : Non-Static
public void nonstatictest(){
System.out.println("This is non-Static method.");
}
//Mehod 2 : Static
public static void statictest(){
System.out.println("This is static method.");
}
//SubClass
public class SubClass{
//Method in SubClass
public void Method_In_SubClass(){
System.out.println("This is Method in SubClass");
}
}
public static void main(String args[]){
Sub SubObject = new Sub();
SubClass SubClassobject = new SubClass();
SubObject.nonstatictest();
statictest();
SubClassobject.Method_In_SubClass();
}
}
Error:
Sub.java:25: error: non-static variable this cannot be referenced from a static context
SubClass SubClassobject = new SubClass();
^
1 error
Then I changed Method_In_SubClass to static but getting this error
Error :Illegal static declaration in inner class Sub.SubClass
public static void Method_In_SubClass(){
^
modifier 'static' is only allowed in constant variable declarations
Sub.java:25: error: non-static variable this cannot be referenced from a static context
SubClass SubClassobject = new SubClass();
Both Main and SubClass are members of class Sub and you cannot reference a non-static member of class sub that is SubClass in Main which is a static member. You need make the entire SubClass into static instead of just the Method_In_SubClass().
The Easiest way to make it work is by making SubClass as Static
public static class SubClass{
//Method in SubClass
public void Method_In_SubClass(){
System.out.println("This is Method in SubClass");
}
}
I assumed that initiating SubClass Object/Instance of the class would be same as Parent class but it's not
SubClass SubClassobject = new SubClass();
The correct way of initiating an object of the subclass is :
Sub.SubClass SubClassobject = SubObject.new SubClass();
Related
This question already has answers here:
"Non-static variable this cannot be referenced from a static context" when creating an object
(5 answers)
Closed last year.
import java.util.*;
public class MyClass {
MyClass()
{
}
public static void main(String args[]) {
Node n = new Node();// CT error - non-static variable this cannot be referenced from a static context
MyClass obj = new MyClass();//works (Why? Since this is also a non-static)
test t = new test();//works (Why? Since this is also a non-static)
}
class Node{
};
}
class test{
}
How can main method (which is static) call it's own class' constructor even if it is non-static? And can't call nested class' constructor.
It's important to note the distinction between calling a static method, which you can do whenever, and calling a non-static method, which must be done on an instance.
class Foo {
public void run() {
System.out.println("Hello, world!");
}
public static void static_run() {
Foo f = new Foo();
f.run();
}
public static void main(String[] args) {
Foo.static_run(); // Calling a static method from a static context
(new Foo()).run(); // Calling a non-static method on an instance of Foo
Foo.run(); // Error! Cannot call a non-static method from a static context
A a = new Foo.A(); // This is fine since A is a static class
B b1 = new Foo.B(); // Error! B is non-static but is being called from a static context
B b2 = new (new Foo()).B(); // This is fine since B is created on an instance of Foo, not from a static context
C c = new C(); // This is fine since top-level classes are implicitly static (i.e. they must be static)
}
static class A {}
class B {}
}
class C {}
Notice how we can create instances of our class within it's own static methods.
class A { //1st code starts here
private void display() {
System.out.println("A class");
}
}
class B extends A {
protected void display() {
System.out.println("B class");
}
}
class Test {
public static void main(String args[]) {
A obj = new B();
obj.display();
}
}
Output : Test.java:22: error: display() has private access in A
obj.display();
class Outer{ //2nd Code starts here
class Inner1{
private void m2() {
System.out.println("Inner1 class");
}
}
class Inner2 extends Inner1{
protected void m2() {
System.out.println("Inner2 class");
}
}
public static void main(String args[]) {
Outer o=new Outer();
Outer.Inner1 i=o.new Inner2();
i.m2();
}
}
Output : Inner1 class
Why compile time error in 1st code while output Inner1 class in 2nd code???
The code of the Outer class can access any member or method declared within the Outer class, regardless of the access level. However, the m2 method being called is the method of the base class Inner1, since you can't override a private method.
On the other hand, the code of the Test class cannot access a private method of a different class, which is why that code doesn't pass compilation.
Because private members are accessible only in class.
when class B extends A private members are inaccessible For B.
In case of Inner classes, An inner class is a member of class and have access to all members of enclosing class.
This question already has answers here:
Why can't we have static method in a (non-static) inner class (pre-Java 16)?
(15 answers)
Closed 8 years ago.
Im trying to understand how inner class works and while experimenting with some simple code i got a error : The method hello cannot be declared static; static methods can only be declared in a static or top level type
on this code
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
static public void hello() {
System.out.println("show class");
}
}
C2.hello();
}
}
and i cant understand why!
Refer to the documentation here.
Inner Classes: As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Class2 is an inner class which means that it needs to be tied to an Class1 object. Then objects of Class2 can access the fields of the bound object at all times:
public class Class1 {
private String name = "class1";
public static void main(String[] args) {
Class1 a = new Class1();
Class2 c = a.new Class2();
c.show();
}
class Class2 {
public void show() {
System.out.println("helloworld: "+name); //accessing the name field of a without needing the variable
}
}
}
or you need to make Class2 static so it doesn't need the Class1 instance.
public class Class1 {
public static void main(String[] args) {
Class2 c = new Class2();
c.show();
}
static class Class2 {
public void show() {
System.out.println("helloworld");
}
}
}
Class C2 in your example above is a local Inner class, which means an inner class defined within a method of an outer class, and such classes cannot have static methods inside them because they are associated with objects, (static methods are not dependent upon objects).
Moreover, a local inner class must be instantiated within the method it has been created and not outside the method. This is a rule.
try modifying your code in following way:
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
public void hello() {
System.out.println("show class");
}
}
C2 obj= new C2();
obj.hello();
}
}
This should work.
You cant do this since you need to create an instance of Class 'Class1' before you can access Class 'C2'. However the method 'hello' should be possible to access without creating an instance (being a static method).
This question already has answers here:
Why can't we have static method in a (non-static) inner class (pre-Java 16)?
(15 answers)
Closed 8 years ago.
Im trying to understand how inner class works and while experimenting with some simple code i got a error : The method hello cannot be declared static; static methods can only be declared in a static or top level type
on this code
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
static public void hello() {
System.out.println("show class");
}
}
C2.hello();
}
}
and i cant understand why!
Refer to the documentation here.
Inner Classes: As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Class2 is an inner class which means that it needs to be tied to an Class1 object. Then objects of Class2 can access the fields of the bound object at all times:
public class Class1 {
private String name = "class1";
public static void main(String[] args) {
Class1 a = new Class1();
Class2 c = a.new Class2();
c.show();
}
class Class2 {
public void show() {
System.out.println("helloworld: "+name); //accessing the name field of a without needing the variable
}
}
}
or you need to make Class2 static so it doesn't need the Class1 instance.
public class Class1 {
public static void main(String[] args) {
Class2 c = new Class2();
c.show();
}
static class Class2 {
public void show() {
System.out.println("helloworld");
}
}
}
Class C2 in your example above is a local Inner class, which means an inner class defined within a method of an outer class, and such classes cannot have static methods inside them because they are associated with objects, (static methods are not dependent upon objects).
Moreover, a local inner class must be instantiated within the method it has been created and not outside the method. This is a rule.
try modifying your code in following way:
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
public void hello() {
System.out.println("show class");
}
}
C2 obj= new C2();
obj.hello();
}
}
This should work.
You cant do this since you need to create an instance of Class 'Class1' before you can access Class 'C2'. However the method 'hello' should be possible to access without creating an instance (being a static method).
What is the example of indirect access to private member of superclass from subclass?
A nested class has access to all the private members of its enclosing
class—both fields and methods. Therefore, a public or protected nested
class inherited by a subclass has indirect access to all of the
private members of the superclass.
Quote from http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
In the quote, we talk about "nested" class
here is an example of how an inner class can access private fields of the outer class.
class OuterClass {
private int x = 7;
public void makeInner(){
InnerClass in = new InnerClass();
in.seeOuter();
}
class InnerClass {
public void seeOuter() {
System.out.println("Outer x is " + x);
}
}
public static void main(String[] args) {
OuterClass.InnerClass inner = new OuterClass().new InnerClass();
inner.seeOuter();
}
}
Finally, if you extend a class with the InnerClass, they will also access the private fields of the OuterClass if your InnerClass is public or protected
It is to be supposed (but the compiler does not enforce it, only warns), that a private method will end being used by a public, protected or default method (otherwise it is useless).
So, the extending class can "indirectly" call the private method by calling the public, protected or default method that ends calling the private method.
Yes, we can access private members of a superclass in the child class through the public method of the superclass which can be invoked from the child class's reference variable heaving the reference id of child class.
for example:-
class Base
{
private int x=10;
void show()
{
System.out.println(x);
}
}
class Child extends Base
{
public static void main(String... s)// public static void main(String[] args)
{
//rom jdk 1.7 main can be defined like above
Child c=new Child();
c.show();
}
}
The output will be 10