-2
    package helloworld;
    public class Helloworld {
               public static void main(String[] args) {
               private int n; 
               n = 25;
               System.out.println("hi"+n);       
            }
}

I get this error while i run : this is caused becuae of private keyword. works if i remove it. Thanks

    Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.ClassFormatError: Method "<error>" in class helloworld/Helloworld has illegal signature "(Ljava/lang/Object;)Lprintln;"
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
/Users/rafi/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
ski
  • 3
  • 3

3 Answers3

2

The import statement can be used only to specify the classes within a package. But java.lang is a package. The compiler implicitly imports the classes in java.lang, it can be omitted from the code.

Sneha
  • 39
  • 6
  • can u tell me why im getting error while using a access modifier – ski Feb 13 '17 at 19:38
  • access modifiers (private,public,protected) are specified on instance/class variable. int n is a local variable and accessible within main method only. so declaring it without private would solve the issue. – Sneha Feb 13 '17 at 19:59
  • yes in the text book im reading they use private on string, int etc... so it inst accessible to other classes. only that class can access it. so i should be able to declare it as private. – ski Feb 13 '17 at 20:32
  • if you want to declare n as private, please declare it as an instance variable. public class Helloworld { private static int n; public static void main(String[] args) { n = 25; System.out.println("hi"+n); } – Sneha Feb 13 '17 at 20:39
0

Some basic theory:

Instance: in a simple manner it can be explained as a copy of your class will all its features.

Static variable: variable belonging to class

Instance variable: variable belonging to instance

Local variable: variable belonging to a particular method

now the important thing

Each instance of the class has its own copy of all the instance variable but only a single copy of static variables is shared by all instances. Further static methods also belong to the class and thus cannot change the value of any instance variable without knowing about the instance.

so we can say static methods can directly call other static methods instance methods can directly call static methods instance methods can directly call other instance methods but for a static method to call any instance method or use any instance variable, we need a instance this instance may be passed to the method or a new instance can be used.

To create a new instance of the class you use the 'new' keyword as

<class-name> <instance-name> = new <class-name>();

like

Test test = new Test();

here test is the name of the instance

further to answer your question variables can be declared inside a method but they shall be local to the method only. they won't be treated as instance variable.

Local variables CANNOT be marked as private, protected or public they are local to the method or the block in which they are declared and will not be visible anywhere outside it.

You are getting an error because you have marked a local variable as private


public class Test {


    private static int m;// this is a static variable

    private int n;// this is an instance variable

    public static void main(String[] args) throws Exception {

        printM();//no need for any instance as printM() is static


//      since 'n' is an instance variable it cannot be used without an instance inside a static method
//      creating instance
        Test test = new Test();
        test.printN();//instance is need to call printN()
    }

    private static void printM() {
        m = 25;
        System.out.println("hi from static method 'm' is: " + m);
    }

    private void printN() {
        n = 15;
        System.out.println("hi from instance method 'n' is: " + n);
    }
}
Doc
  • 10,831
  • 3
  • 39
  • 63
-2

What about declaring n as a string and then print out n?

datorre
  • 5
  • 3