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(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









Related Tutorials/Questions & Answers:
java interview question and answer - Java Interview Questions
java interview question and answer   sir, i want one mark java interview question and answers
Interview question link - Java Beginners
Interview question link  Hi, I want JAVA+J2EE interview question and answer please suggesion me link
Advertisements
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
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
interview question - Java Interview Questions
interview question  hello i want technical interview question in current year   Hi Friend, Please visit the following links: http://www.roseindia.net/interviewquestions/ http://www.roseindia.net
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
Interview Question - Java Interview Questions
Interview Question  I need Interview Questions on Java,J2EE Pls help me
Interview question - Java Interview Questions
Interview question   Hi Friends, I am new to java, can u tell me what is class ,object and instance with example. For example : class raja , raja r = new raja(); ,here raja is class and r is instance
Interview question - Java Interview Questions
Interview question   Hi Friends, Give me details abt synchronization in interview point of view. I mean ow to explain short and neat. Thanks
Interview Question - Java Interview Questions
Interview Question  1)If we give S.O.P. statement after finally block shall it prints or not? 2)Write a program in java to read a file & write in to another file? 3)Write a program taking two arrays and compare those two
Core Java Interview Question, Interview Question
Core Java Interview Question Page 27       Modifiers Question: What are access modifiers... modifier then it is considered as Friendly Question: Can protected or friendly
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
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 18       Question: Is &&= a valid Java... different sizes. Question: What advantage do Java's layout managers provide over
Core Java Interview Question, Interview Question
Core Java Interview Question Page 16  ..._TO_REPLACE_3 Question: How are Java source code files named? Answer: A Java source code... the .java extension. ADS_TO_REPLACE_4 Question: What is the relationship between
Core Java Interview Question, Interview Question
Core Java Interview Question Page 15  ... to the Java runtime system. Question: How many times may an object's finalize...: A program's main() method takes an argument of the String[] type. Question: Which Java
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. ADS_TO_REPLACE_1 Question: Which class
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... Question: How would you make a copy of an entire Java object with its state
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. ADS_TO_REPLACE_1 Question: What an I/O
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. ADS_TO_REPLACE_1 Question
Core Java Interview Question, Interview Question
Core Java Interview Question Page 13       Question: What is an Iterator interface? Answer.... ADS_TO_REPLACE_1 Question: What is the difference between the >> and >>
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 20       Question: Name four Container classes. Answer..._TO_REPLACE_1 Question: What is the difference between a Choice and a List? Answer
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. ADS_TO_REPLACE_1 Question: What
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
Core Java Interview Question, Interview Question
Core Java Interview Question Page 23  ... group. ADS_TO_REPLACE_3 Question: What is a Java package and how is it used... that are loaded by a Java program. ADS_TO_REPLACE_5 Question: How does a try statement
one interview question - Java Interview Questions
one interview question   Hello, In one of my interview the interviewer ask one question "what are the problems you faced during project... for this question. Thanks
Core Java Interview Question, Interview Question
Core Java Interview Question Page 26  ... explicitly Question: How is an argument passed in java, by copy... Question: What are different types of operators in java Answer: Uniary
Core Java Interview Question, Interview Question
Core Java Interview Question Page 24  .... Question: What is reflection in java Answer: Reflection allows Java code..., it is read only Question: What is the difference between Java Bean and Java
Core Java Interview Question, Interview Question
Core Java Interview Question Page 25  ... function would be called. Question: Does java support virtual functions.... A Java program requires a JVM to run on a particular platform Question: Can
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 29       Flow Control and exception Question: What... at the end of the body Question: When do you use continue and when do you use
Core Java Interview Question, Interview Question
Core Java Interview Question Page 28       Conversion Casting and Promotion Question: What... primitive types to be accessed as objects. Question: What are the four general
Core Java Interview Question, Interview Question
Core Java Interview Question Page 4       Question: What is the difference between checked and Unchecked Exceptions in Java ? Answer: All predefined exceptions in Java
Core Java Interview Question, Interview Question
Core Java Interview Question Page 31       Threads Question: Where does java thread... The java language and virtual machine Question: What is the difference
Core Java Interview Question, Interview Question
Core Java Interview Question Page 9       Question: What are the uses of Serialization...(ts[i]); } } }ADS_TO_REPLACE_8 Question: What methods java providing for Thread
Core Java Interview Question, Interview Question
Core Java Interview Question Page 5       Question: What are different types... is a direct member of a package. Inner classes, which became available with Java
Core Java Interview Question, Interview Question
Core Java Interview Question Page 7       Question: What... of java object into output stream, that stream can be file or byte array or stream
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 10       Question: What is the difference between notify..._TO_REPLACE_1 Question: What is synchronized keyword? In what situations you will Use
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 6       Question: How can i tell what state a thread...++){ System.out.println(ts[i]); } } } Question: What methods java providing
struts interview Question - Struts
struts interview question and answer java  struts interview question and answer java
core java question - Java Interview Questions
core java question  I Have been asked the question in interview why we use constructors????? or what is the need of constructors?? or what is the utility of constructors????  to intialize the instance variabe we
Core java interview question, object creation.
Core java interview question, object creation.  How can we restrict to create objects for more than five? That means i want only 5 objects, how to restrict to create 6th objects
Spring question - Java Interview Questions
Spring question  Hi Pls any one give idea abot this question? in the above statement, what is the meaning og abstarct = true? Thanks In Advance   Hi friend, Read for more information. http
Interview question.
Interview question.  Program to read the integers from the standard input (until it gets a negative number) and puts them in to an array.after that it calls process Array on the array,and then prints the content of the array

Ads