CrazyEngineers
  • Newbie Java Doubts

    Updated: Oct 22, 2024
    Views: 1.1K
    Ok,so finally I have started learning java..and I have some newbie doubts which can annoy the answerer(they can be a lil dumb)..Please help me out CEans!😀

    1.Can we not define main function outside the class?
    2.Can a class having main function have other members too?
    3.Can main function be defined in a class whose object is created within main?
    4.What actually object holds,is it a pointer(I know the concept of object,just asking about its memory representation?
    5.Why do we need to define get and set functions,why don't we simply make the variables public?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • rahul69

    MemberMay 2, 2013

    #-Link-Snipped-#
    4: An object, when created actually has memory, it is not a pointer(however we can also create pointer to objects). It's concept is similar to any other variable of basic data type.

    5:Making variables public, among other things, defeats the higher cause😁. In simple language it will violate fundamental concepts of OOP, ie encapsulation, data hiding, abstraction etc.
    Regarding 1,2,3 you can try it yourself to see what happens actually😉.
    Eg for 1. you can define main function outside the class and compile the program to see what happens, it will give you better insight and experience to avoid small mistakes in the future.
    Good Luck!
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberMay 2, 2013

    #-Link-Snipped-#
    You need to get concepts of Object -oriented concepts first. There is simple rule to learn java is to read the chapters and then start writing programs along with.
    Java strictly follow Object-oriented concepts (leave some exceptions)..
    Class: Represent an entity from which we create an object known as object initialization.
    functions Method: behavior of class or let say object.using which a object do some work.
    Class Variables: properties of class.

    Now 1: how can you do work without any real entity. Ex: you need an mouse object to do click (method).
    2- didn't get it.
    3:Ideally you can create any object of public/ accessible class in main method. For learning purpose we write only one class and run it , that is why there is just one class and one method main() and create object of that class in main().
    4: Object is a memory space which hold a unique identity of that class.
    5:There are numerous use of set and get one which is, Getter and setters are used to create a separation of layer. you want a service you just pass values in setMethod and inside that value will fetch by getMethod to perform some action.
    For more see this #-Link-Snipped-# .

    In starting of java, do not push yourself why and how, just keep practicing more programs. further you sail, you will start understanding why things are happening.
    Are you sure? This action cannot be undone.
    Cancel
  • Anand Tamariya

    MemberMay 2, 2013

    5: At times you may want a read-only property. To achieve that, you can provide a getter but no setter.
    Are you sure? This action cannot be undone.
    Cancel
  • Whats In Name

    MemberMay 3, 2013

    #-Link-Snipped-# Thanks for the help 😀 ..


    rahul69
    #-Link-Snipped-#
    4: An object, when created actually has memory, it is not a pointer(however we can also create pointer to objects). It's concept is similar to any other variable of basic data type.
    Is memory allocated when object is created or when it is assigned data values?

    rahul69
    #-Link-Snipped-#
    Making variables public, among other things, defeats the higher cause😁. In simple language it will violate fundamental concepts of OOP, ie encapsulation, data hiding, abstraction etc.
    When we create get and set methods,doesn't the fundamental concepts still violated?Can't we still access the data variables?
    Are you sure? This action cannot be undone.
    Cancel
  • Whats In Name

    MemberMay 3, 2013

    ianoop
    #-Link-Snipped-#


    4: Object is a memory space which hold a unique identity of that class.
    Does this means,object is holding a reference or address of some unique class?
    ianoop
    #-Link-Snipped-#

    In starting of java, do not push yourself why and how, just keep practicing more programs. further you sail, you will start understanding why things are happening.
    You are right!
    Whenever I start a new thing to learn about,I am always stuck with the basic (sometimes irrational) doubts,Can't help myself! and then it degrades my interest in learning.😕Well for Java,I will try to avoid this.Thanks again for answering my queries😀
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberMay 3, 2013

    Whats In Name
    Does this means,object is holding a reference or address of some unique class?
    When you write ClassABC objABC = new ClassABC();
    that means a memory will be allocated to an object of ClassABC and there will be a reference objABC, by which you can access that object.
    Are you sure? This action cannot be undone.
    Cancel
  • Whats In Name

    MemberMay 3, 2013

    ianoop
    When you write ClassABC objABC = new ClassABC();
    that means a memory will be allocated to an object of ClassABC and there will be a reference objABC, by which you can access that object.
    I understood it finally...thanks😀
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberMay 4, 2013

    Whats In Name
    Is memory allocated when object is created or when it is assigned data values?
    Memory is allocated when object is created (as at that time, constructor is called)
    Whats In Name
    When we create get and set methods,doesn't the fundamental concepts still violated?Can't we still access the data variables?
    With get and set , We can access variables, but in a restricted manner, for example if we are not changing a private data member via get method, there is no chance of changing it accidentally, but if all variables are directly accessible, then there are chances of modifying their values by mistake, which is not desirable.
    Are you sure? This action cannot be undone.
    Cancel
  • Whats In Name

    MemberMay 4, 2013

    rahul69
    Memory is allocated when object is created (as at that time, constructor is called)

    With get and set , We can access variables, but in a restricted manner, for example if we are not changing a private data member via get method, there is no chance of changing it accidentally, but if all variables are directly accessible, then there are chances of modifying their values by mistake, which is not desirable.
    Thanks a lot!!!..Keep helping😁
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register