java interview question

Ads
 

java interview question

class Test { void meth1() { System.out.println("meth1()"); meth2(); }

 static void meth2()
 {
       System.out.println("meth2()");
 }

 public static void main(String[] args)
 {
         Test t=new Test();
          t.meth1();
  }

}

query: In the above program in meth1() the static method method2() is called. How meth2() is called?with object reference((t)(this))) or with class name(Test)? When i tried to print this (System.out.println(this);) inside meth2() it is giving error non static variable this can't be referenced from static context.

what i know : To call a non static method from a non static method no need of any object reference.we can directly call. The calling method uses this and calls.

                In case of calling static method from static , we can directly 
                call(here class name will be used) or we can use object reference.

                when calling static method from non static method,there are again 
                two chances only,calling with object reference(this) or class name.
                If it uses current object( this ) it should allow to print this inside that class na?
View Answers

January 31, 2011 at 3:51 PM

Hi Friend,

Try this:

class Test {
    void meth1() { 
        System.out.println("meth1()");
        meth2(); 
        }

 static void meth2()
 {
       System.out.println("meth2()");
 }

 public static void main(String[] args)
 {
         Test t=new Test();
          t.meth1();
          meth2();
  }
}

Thanks

Ads









Related Tutorials/Questions & Answers:
Java Interview Question
Java Interview Question  Java program Interview Question :i have a file which contains characters and integers in mixed format. for example :--> abd12 g156bcd etc Now how would i rearrange the file sothat the characters
Core Java Interview Question, Interview Question
Core Java Interview Question   ....   Core Java Interview Question Page 1 How could Java... to a file?      Core Java Interview Question Page 2
Advertisements
java interview question
java interview question  when will we use abstraction or interface
java interview question
java interview question  what is the usage of abstract class
Core Java Interview Question, Interview Question
Core Java Interview Question Page 14       Question: What is clipping? Answer: Clipping... that is implemented in a language other than Java. Question: Can a for statement loop
Core Java Interview Question, Interview Question
Core Java Interview Question Page 21       Question: How are the elements of a GridLayout... and are laid out using the squares of a grid. Question: What an I/O filter? Answer: An I
Core Java Interview Question, Interview Question
Core Java Interview Question Page 17       Question: What is the difference between... application window that can have a menu bar. Question: Which class is extended by all
Core Java Interview Question, Interview Question
Core Java Interview Question Page 23       Question: To what value is a variable... and interfaces that are loaded by a Java program. Question: How does a try statement
Core Java Interview Question, Interview Question
Core Java Interview Question Page 22       Question: What is a compilation unit? Answer: A compilation unit is a Java source code file. Question: What interface
Core Java Interview Question, Interview Question
Core Java Interview Question Page 2   .... Question: What comes to mind when you hear about a young generation in Java... mentions a shallow copy in Java? Answer: Object cloning. Question: If you're
Core Java Interview Question, Interview Question
Core Java Interview Question Page 18       Question: Is &&= a valid Java operator? Answer: No, it is not. Question: Name the eight primitive Java
Core Java Interview Question, Interview Question
Core Java Interview Question Page 27       Modifiers Question: What are access modifiers... written in non java language Question: What are transient variables
Core Java Interview Question, Interview Question
Core Java Interview Question Page 26       Language Fundamentals Question: How... 64 - - Question: What types
Core Java Interview Question, Interview Question
Core Java Interview Question Page 16  ... implementation is deferred to a subclass. Question: How are Java source code files... and interfaces. Source code files use the .java extension. Question: What
Core Java Interview Question, Interview Question
Core Java Interview Question Page 12       Question: What is a transient variable? Answer... collections of objects. Question: How does Java handle integer overflows
Core Java Interview Question, Interview Question
Core Java Interview Question Page 15  ... is to provide access to the Java runtime system. Question: How many times may... takes an argument of the String[] type. Question: Which Java operator is right
Core Java Interview Question, Interview Question
Core Java Interview Question Page 32   ... Question: What is the ultimate ancestor of all java classes Answer: Object class is the ancestor of all the java classes Question: What
java interview question
java interview question  class Test { void meth1() { System.out.println("meth1()"); meth2(); } static void meth2() { System.out.println("meth2()"); } public static void main
java interview question
java interview question  class Test { void meth1() { System.out.println("meth1()"); meth2(); } static void meth2() { System.out.println("meth2()"); } public static void main
Important Java Interview Question
Important Java Interview Question  Complete the Solver constructor so that a call to solveAll return a list with 2 values including the square root and the inverse of the integer passed as parameter. public interface
Core Java Interview Question, Interview Question
Core Java Interview Question Page 13       Question: What is an Iterator interface? Answer.... Question: What is the difference between the >> and >>> operators
Core Java Interview Question, Interview Question
Core Java Interview Question Page 30       Objects and Classes Question: What's... while regular methods could be called many times. Question: What
Core Java Interview Question, Interview Question
Core Java Interview Question Page 19       Question: What is the difference between static... variables take on unique values with each object instance. Question: What
Core Java Interview Question, Interview Question
Core Java Interview Question Page 20       Question: Name four Container classes. Answer: Window, Frame, Dialog, File Dialog, Panel, Applet, or ScrollPane Question: What
Core Java Interview Question, Interview Question
Core Java Interview Question Page 25  ... be called. Question: Does java support virtual functions Answer: No java... Question: What is JVM Answer: When we install a java package
Core Java Interview Question, Interview Question
Core Java Interview Question Page 24       Question: Why do we need public static void main.... Question: What is reflection in java Answer: Reflection allows Java code
Core Java Interview Question, Interview Question
Core Java Interview Question Page 11       Question: What is serialization ?  Answer: Serialization is the process of writing complete state of java object
Core Java Interview Question, Interview Question
Core Java Interview Question Page 4   ... of the class object: Java- class-name.field-or-method-name Question: What.... Java Code private static final int MAXATTEMPTS = 10; Question
Java interview question
Java interview question  Hello i had an online test in some company and i didn't succeed in doing it(they gave me a timeline) this was the test... and said to do it in Java(Regular java programming),but I didn't know how
Core Java Interview Question, Interview Question
Core Java Interview Question Page 9       Question: What are the uses of Serialization... computer using such client/server Java technologies as RMI or socket programming
Core Java Interview Question, Interview Question
Core Java Interview Question Page 29       Flow Control and exception Question: What... at the end of the body Question: When do you use continue and when do you use

Ads