Monday, February 24, 2014

Creating object for class and accessing methods of class. Various forms of Object



- Delcare object at class level:
See in order to use the object created in main method in all other methods in the same class, we need to declare object as class instance.

       Note: if you dont declare object at class level, then we cannot use it in multiple methods.

    public class DemoQAsitetesting {

private static WebDriver webDriverobj;  //declared object

public static void main(String[] args) {

webDriverobj = new FirefoxDriver(); //WebDriver webDriverobj=new FirefoxDriver();  ...if we declare this we were not able to use object webDriverobj in other methods.

}

public void registration(){
     
webDriverobj.get("http://www.demoqa.com");
          }


-
//Creating object for class and accessing methods of class

package javaFAQsolutions;

public class objectsandmethods {

    String empname="Sreekanth1";
    int empsal;
   
   
    public static void main(String arg[]){
       
        objectsandmethods obj1 = new objectsandmethods();

        obj1.fetch(); //obj1 is nothing but a copy of class, so should be able to access its methods
        obj1.getEmpName();
    }
    private void fetch(){
        String empname="Sreekanth";
        System.out.println(empname);
           
    }
   
   
    public void getEmpName(){
        objectsandmethods obj2=new objectsandmethods();
        System.out.println(obj2.empname);
        //System.out.println(obj1.empname);
       
    }
}



12/22/2015:


See how object is being used:

Source: http://docs.oracle.com/javase/tutorial/java/data/characters.html

Most of the time, if you are using a single character value, you will use the primitive char type. For example:
char ch = 'a'; 
// Unicode for uppercase Greek omega character
char uniChar = '\u03A9';
// an array of chars
char[] charArray = { 'a', 'b', 'c', 'd', 'e' };
There are times, however, when you need to use a char as an object—for example, as a method argument where an object is expected. The Java programming language provides a wrapper class that "wraps" the char in a Character object for this purpose. An object of type Charactercontains a single field, whose type is char. This Character class also offers a number of useful class (i.e., static) methods for manipulating characters.
You can create a Character object with the Character constructor:
Character ch = new Character('a');

6 comments:

  1. Replies
    1. I think while declaring object java will use the memory allocated for class for that object. Such as: A and B are Classes.

      A obj1 = new A()
      A obj2 = new B() --- if B is implementing Class A, here memory of class B is used to declare object of class A.

      Delete
  2. Source: http://stackoverflow.com/questions/14816554/java-namespace-collisions

    public class Foo{

    Bar bar;

    public void doSomething(){
    Bar bar = makeNewBar();
    // bla bla
    }

    }

    I don't understand the above code, why the method and object together?

    ReplyDelete
  3. What does passing parameter while creating object mean?

    Ex:
    classname obj1 = new classname("df","gfd")

    ReplyDelete
  4. What does the below mean:

    ClassA obj1 = new ClassB()

    ReplyDelete
    Replies
    1. Here obj1 is an object of ClassA, created by using memory of ClassB.

      It is mostly used when ClassB is implementing ClassA, as interface does not allow to create object. ClassA is a interface here.

      so methods in ClassA could be separately accessed using obj1..

      ClassB obj2 = new ClassB();

      And obj2 here has access to both methods of ClassA and ClassB.

      Delete