Fix - Cannot Declare Variable Of Static Type

First of all we will see what type of statement causes this error to occur. We all know that we cannot create object of type static as this cannot be instantiated and also we cannot declare a new keyword to create object and also we can't assign it to a variable by doing this.
 
Now let's check out the code for doing this.
 
We have this static class with static method. 
  1. public static class StaticClass  
  2. {  
  3.       public static void staticMethod()  
  4.       {  
  5.        // to do code  
  6.       }  
  7. }    
And there is another class and method inside which we want to use. 
  1. public class AotherClass {  
  2.             public void AnotherClassMethod() {  
  3.   
  4.             }  
  5.         }   
so what we want to do to call this method. let's say we want to do like this.
 
After instantiating it with a new keyword and declaring variable to do it normally with class it gives us the result we are talking about. We cannot declare a variable of static type.

Check this image for details.
 


Also it gave another error beside saying it cannot create instance of static class. It's the error but let's discuss how to fix this. 
 
We cannot declare a variable or instance of static class which means we cannot do it for this static type of class , but despite this we call the static class by name to acccess its members. 
  1. public class AotherClass {  
  2.            public void AnotherClassMethod() { 
  3. // Call the static class method by static class name . method name 
  4.                StaticClass.staticMethod();  
  5.                  
  6.            }  
  7.        }  
So this is it for today. Hope you find it useful. 
Ebook Download
View all
Learn
View all