Results 1 to 8 of 8
  1. #1
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default [SOLVED] Class-level vs Object-level method()

    Hello everyone,
    Can anyone please explain with codes(if you can plz) how can we create object-level method().....
    we can create class level method
    like
    class A{
    private String name;
    //class level method
    public void setName(String n){
    name = n;
    }
    //class level method
    public String getName(){
    return name;
    }
    }


    ...............

    //instantiating object
    A a1 = new A();

    Here I'm confused how can we create object level method!!!!!!!!:confused:

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    What do you mean by class/object level methods? All methods are defined as being part of some class or other.

    Or do you mean how do you invoke a method for a particular object once you have instantiated that object? If so, it's like this:

    Java Code:
    class A {
        private String name;
        public void setName(String n) {
            name = n;
        }
        public String getName() {
            return name;
        }
    
        public static void main(String args[]) {
            A a1 = new A();
            a1.setName("Brian");
    
            System.out.println("Hello world, my name is " + a1.getName());
    
            A a2 = new A();
            a2.setName("Bob");
            System.out.println(
                    "... and my name is " + (a2.getName().equals(a1.getName()) ? "also" : "not"));
        }
    }
    As the second line of output shows, the getName()/setName() methods are defined for the class, but they have an effect that is particular to the object for which they are called.

  3. #3
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    No, not the invoke one. I got this question

    Write down the list of methods required to model the procedural component of a customer, i.e. Operations/ behaviour that a customer can perform.
    For each of these methods:
    Would you model it as a class-level/object-level method? Why?

    here they are asking me to which method I would model it as class/ object level method which is very confusing even I also know all methods are created within the class but object level!!!!

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Would you model it as a class-level/object-level method? Why?
    I'm guessing this means static or non-static methods.
    IOWs:
    Would you model it as a static methods or non-static methods? Why?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    object-level method:
    when it makes sense that the behaviour should occur in individual objects
    e.g. deposit of money into a bank account
    you must specify which bank account to deposit, not to all objects in the same class!
    class-level method:
    usually as utility methods
    e.g. the String class has a method parseInt(…) that converts a string into an int value

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    So object-level methods are indeed non-static methods, and class-level methods are static methods.

    The real difference here is this...
    object-level(non-static) methods would be invoked like this:
    Java Code:
    MyClass myObject = new MyClass();
    myObject.MyObjectClassObjectLevelMethod();
    whereas class-level(static) methods would be invoked like this:
    Java Code:
    MyClass.MyObjectClassClassLevelMethod();
    Also, the Integer class has a parseInt(String) method, the String class does not.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default

    After reading length of pages the concept which I grasp is that Object-level whose value can be changed later on. where as Class-level whose value constant to all instantiated objects such as interest rate. If we declared a attribute within a class called interestRate = 2% which will be constant to all object instantiated and fname,lname such attributes change accordingly so they called as object-level

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Any value that isn't "final" - whether static or not - can be changed later on. Don't interest rates rise and fall?

    The important thing is that static values are shared - or, better, held in common - by all instances of a class. The same thing goes for static methods: they are bits of behaviour that are shared by all instances and, in particular, only depend on static values.

    In Java they usually go by the name static - in honour of the keyword with which they are declared. By in other contexts you may see the distinction made between class variables and methods and instance variables and methods. AFAIK the ???-level terminology is not standard.

    [Edit] I forgot the most useful to know about static variables and methods: don't use them.

    "Write down the list of methods required to model the procedural component of a customer, i.e. Operations/ behaviour that a customer can perform". Since you talking about the behaviour of an instance of customer those methods are going to be non static.
    Last edited by pbrockway2; 06-23-2009 at 10:22 AM.

Similar Threads

  1. importing packages at the same level
    By Bharat in forum New To Java
    Replies: 3
    Last Post: 03-30-2009, 04:17 AM
  2. Annotations to change access level of method
    By jwilley44 in forum Advanced Java
    Replies: 1
    Last Post: 02-16-2009, 07:52 AM
  3. Row level locking........
    By jithan in forum New To Java
    Replies: 0
    Last Post: 09-02-2008, 08:09 AM
  4. row level locking
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-28-2008, 07:42 PM
  5. Field Level Security
    By narayanasgs in forum Web Frameworks
    Replies: 0
    Last Post: 05-30-2008, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •