Can we declare a static class inside a static function in java? - java

What is wrong with the bellow code?
And if something is wrong, please explain in detail with all the rules of static class and functions in java.
public class inner
{
static class in
{
static void inclass()
{
static class infunclass
{
static void fun()
{
System.out.println("Infunclass");
}
}
infunclass.fun();
}
static void infun()
{
System.out.println("Infun");
inclass();
}
}
static void fun()
{
System.out.println("fun");
in.infun();
}
public static void main(String[] args)
{
fun();
}
}
Now when I remove the static keyword before the inclassfun, it showing a different error
public class inner
{
static class in
{
static void inclass()
{
class infunclass
{
static void fun()
{
System.out.println("Infunclass");
}
}
infunclass.fun();
}
static void infun()
{
System.out.println("Infun");
inclass();
}
}
static void fun()
{
System.out.println("fun");
in.infun();
}
public static void main(String[] args)
{
fun();
}
}
static void fun()
^
modifier 'static' is only allowed in constant variable declarations

Can we declare a static class inside a static function in java?
No. The JLS forbids it. The governing parts of the specification are as follows:
"The modifier static pertains only to member classes (§8.5.1), not to top level or local or anonymous classes." JLS 8.1.1
"A member class is a class whose declaration is directly enclosed in the body of another class or interface declaration (§8.1.6, §9.1.4). " JLS 8.5
A class declared directly within a method is a local class, not a member class

Related

What are the 3 different contexts a class can be declared?

I am told there are three different contexts in which a class can be declared in Java.
It has to do with the location within a program, but I can't think of what they are.
Obviously a class can be declared at the top of a page, the only other example I can think of is like a nested class?
I feel I may be going about this the wrong way.
In a package
package com.example.mypackage;
public class TheClass {
}
In a class
package com.example.mypackage;
public class OuterClass {
class InnerClass {
}
}
Anonymously
public class MainClass {
public static void main(String[] args) {
AbstractClass myObject = new AbstractClass() {
// overrides and other fields of the
// anonymous class goes in this block
};
}
}
EDIT: As #daniu stated in the comment, a class can also be created in a method:
public class MainClass {
public static void main(String[] args) {
class MethodClass {
}
MethodClass myObject = new MethodClass();
}
}
There are actually 4 distinct syntactic contexts:
In the "compilation-unit" context; i.e. a top-level class declaration.
package foo;
public class Bar{}
As a nested class declaration in a class declaration.
package moo;
public class Cow {
public class Inner {}
}
As a nested class declaration in a method declaration.
package too;
public class Far {
public void test() {
class MethodInner{}
}
}
As an anonymous class declaration in a new expression. The expression could appear in a variety of contexts. For example:
package goo;
public class Tar {
public void test() {
Runnable r = new Runnable() {
public run() { }
};
}
}
This may be what you are looking for:
public class TopLevel {
public static void main(String[] args) {
TopLevel topLevel = new TopLevel();
Nested nested = new TopLevel.Nested();
Inner inner = topLevel.new Inner();
}
public static class Nested {
}
public class Inner {
}
}

Why can't I acces the public variable from the Test class in the main function?

I am new at learning Java and I do not understand why I can't println the variable prvvar. Why does it need to be set to static?
package myproject;
class Test
{
private int prvvar=2;
public int pbvar=3;
}
Main class:
public class bycicleDemo
{
public static void main(String[] args) {
Test da = new Test();
System.out.println(Test.pbvar);
}
}
In Java non static variables are Object associated and static variables Class associated.
In your case pbvar is non static variable, so you can't access it using class name you need to access it using object. As below
public class BycicleDemo
{
public static void main(String[] args) {
Test da = new Test();
System.out.println(da.pbvar);
}
}
If this pbvar would have been declared as static as below then calling with class name was correct.
package myproject;
class Test
{
public static int pbvar=3;
private int prvvar=2;
}
And class name should start with Capital letter BycicleDemo as per Java standards.
Since pbvar is not static you cant access it through the class. Access it through your newly created instance instead System.out.println(da.pbvar);.
If you declare pbvar like this public static int pbvar=3; your example: System.out.println(Test.pbvar); would work.

Why can't interfaces have static initialization block when it can have static methods alone?

After java 8 it is known that interfaces can have static methods and default methods.
Below is the example :
interface interA{
static void method()
{
System.out.println("Static method inside interface");
}
public default void AImp(){
System.out.println("Calling Aimp from interA");
}
}
Now my question is that why is static initialization block not allowed in interface.?
Example :
interface interA{
static void method()
{
System.out.println("Static method inside interface");
}
static
{
}
public default void AImp(){
System.out.println("Calling Aimp from interA");
}
}

How to access anonymous inner class in main method?

How to access the anonymous inner class object in main method. It is giving compile time error saying that "cannot make static reference to non static method". If I am making anonymous inner class as static then I can access ut I want to access without making it static.
How to do that. Please help.
AnonymousInnerClass2.java
abstract class AnonymousInnerClass21
{
abstract void m();
}
public class AnonymousInnerClass2
{
AnonymousInnerClass21 a=new AnonymousInnerClass21()
{
#Override
void m() {
System.out.println("Hello");
}
};
public static void main(String[] args)
{
a.m();
}
}
this is because for accessing inner class (whether its is normal/named class or Anonymous class), you must create object of class in which inner class is defined, you can try below
abstract class AnonymousInnerClass21
{
abstract void m();
}
public class AnonymousInnerClass2
{
AnonymousInnerClass21 a=new AnonymousInnerClass21()
{
#Override
void m() {
System.out.println("Hello");
}
};
public static void main(String[] args)
{
AnonymousInnerClass2 anonymousInnerClass2= new AnonymousInnerClass2 ();//create outer class object
anonymousInnerClass2.a.m(); // access inner class object through outer class object
}
}

In Java why cant we use an inner class with name as String if the parent class has psvm

public class OuterClass {
public static void main(String[] args) {
System.out.println("Hello !");
}
private class String {
int i = 10;
}
}
The above code compiles fine
When I tried to run the above code, it is throwing error as " Error: Main method not found in class , please define the main method as:
public static void main(String[] args)" . Any reason why such a Runtime Exeption occurs ?
Because your inner class has higher visibility than java.lang.String; thus you have changed the main signature. Change your main like
public static void main(java.lang.String[] args) {
System.out.println("Hello !");
}
Or rename your class String.

Categories

Resources