Skip to main content

Methods interview questions and answers in java

A method represents group of statements that performs a task. Here, Task represents a calculation or processing of data or generating a report etc. This page also contains the information of the methods related interview questions and answers in java. If any one are facing more than these interview questions please share with me through "Contact me Here" gadget or drop me an email to "subbareddynallamachu@gmail.com".

Q1: How many types of methods are there in java?

A: In java mainly two types of methods are there. That are,

                1. Instance methods
                2. Static methods
                3. Factory methods

Q2: What are instance method?

A: Instance methods are methods which act on the instance variable of the class. To call the instance methods. We should use the form: objectname.methodname().

Q3: What are static methods?

A: Static methods are methods which do not act up on the instance variable of a class. static methods are declared as static.

Q4: What is the difference between instance variable and Class variable(static variable)?

A: 1. Instance variable is a variable whose separate copy is available to each object. A class variable is a variable whose single copy in memory is shared by all objects.
2. Instance variables are created in the object on heap memory. Class variables are stored on method area.

Q5: Is it possible to compile and run a java program without writing main() method?

A: Yes, it is possible by using the static block in the java program.

Q6: How are objects are passed to methods in java?

A: Primitive datatypes, objects, even object references - everything is passed to methods using 'pass by value' or 'call by value' concept. This means their bit by bit copy is passed to the methods.

Q7: What are factory methods?

A: A factory method is a method that will create and returns an object to the class to which it belongs. A single factory method replaces several constructors in the class by accepting different options from the user, which creating the object.

Q8: In how many ways you can create an object in java?

A: There are 4 ways of creating an object in java:

1. Using new operator

     Employee emp = new Employee();

Here, we are creating Employee class object 'emp' using new operator.

2. Using factory methods

    NumberFormat obj = NumberFormat.getNumberInstance();

Here, we are creating NumberFormat object using the factory method getNumberInstance().

3. Using newInstance() method. Here, we should follow two steps, as

a) First, store the class name Employee as a string into an object. For this purpose, factory method forName() of the class Class will be useful.

      Class c = Class.forName("Employee");

we should note that there is a class with name Class in java.lang package.

b) Next, create another object to the class whose name is the object c. For this purpose we need newInstance() method of the class Class as,

      Employee emp = (Employee)c.newInstance();

4. By cloning an already available object, we can create another object. Creating exact copy of an existing object is called 'cloning'.

      Employee emp1 = new Employee();

      Employee emp2 = (Employee)emp1.clone();

Earlier we created emp2 by cloning the Employee object emp1.clone() method of object class is used to clone an object. We should note that there is a class by the name Object in java.lang package. 

Related posts:

Comments

Popular posts from this blog

JVM, JRE and JDK in Java

JVM, JRE and JDK are the most basic common concepts to know in java. These are the basic features to understand how Java architecture works? JVM stands for Java Virtual Machine, which doesn't have any physical directories created in java installation. JRE stands for Java Runtime Environment, which creates the directory under Java installation path and also present in JDK. JDK stands for Java Development Kit, which creates the directory in Java installation path and also it has it's own JRE. Since we have already learn that Java is platform independent means if we have implemented any of the java class in one environment, it will be executed in any other environment and provides the same output. But, JVM, JRE and JDK all are platform dependent . So that, for windows, linux, unix, mac, solaris..etc has it's own JVM, JRE and JDK. One will be not compatible with other environments. While installing the Java, we might come to know a bit about JRE and JDK. But, JVM is the other...

Prime, Fibonacci and Factorial number with example in java

Prime number, Fibonacci series and Factorial number programs are most commonly asked questions in interview. Read this article to know what is and how to write programs for prime number, fibonacci series and factorial number. Prime Number: prime number is natural number greater than 1 that has no positive divisor other than 1 and itself. A natural number greater than 1 is not a prime number, is called Composite number . For example, 7 is a prime number. Because it can divide with 1 and 7 only. Where as 8 is composite number. Since it has the divisor 2 and 4 in addition to the 1 and 8. The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false. package com . javatbrains . practice ; public class PrimeNumber { public boolean isPrimeNumber ( int number ) { if ( number <= 1 ) return false ; // There's only one ...

How to insert images into database using JDBC?

How to insert images into database table by using JDBC? To know about these topic follow the below lines,           Before going to write a JDBC program for inserting images into database table, we need to create a specific table for inserting images column datatype. For creating the database table, execute the below command in MySQL command prompt. 1 2 3 4 CREATE TABLE ` images ` ( ` image_id ` int ( 11 ) NOT NULL AUTO_INCREMENT, ` image_flag ` int ( 11 ) NOT NULL , ` image ` longblob NOT NULL , PRIMARY KEY ( ` image_id ` ));           After executing the above query in MySQL command prompt, execute the below query to know weather the above table query is created or not. 1 Show tables;            When you have executed the above query that will show you the list of table which all exists in the current database. Current tab...