Newbie Java Doubts

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?

Replies

  • rahul69
    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.

    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!
  • Anoop Kumar
    Anoop Kumar
    #-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.
  • Anand Tamariya
    Anand Tamariya
    5: At times you may want a read-only property. To achieve that, you can provide a getter but no setter.
  • Whats In Name
    Whats In Name
    #-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?
  • Whats In Name
    Whats In Name
    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😀
  • Anoop Kumar
    Anoop Kumar
    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.
  • Whats In Name
    Whats In Name
    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😀
  • rahul69
    rahul69
    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.
  • Whats In Name
    Whats In Name
    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😁

You are reading an archived discussion.

Related Posts

Hello everyone.. My name is Himanshu Juneja and i am new in this forum. I want to share my knowledge to everyone and also get some knowledge from you guys....
Hi guys, I am Calvin Brave, newbie is here. Looking forward to discussing stuff with you all.I have just discovered this website and I hope to contribute a few of...
exam is coming soon on 5 may i need some tips related to this so please tell me about this
hi everyone. i am unable to understand power systems analysis. neither am being able to clear my concepts nor solve any problem. how do i improve? obviously the answer is...
As we know that in Vapor Absorption Refrigeration Cycle the compressor part is replaced by a system including absorber, pump, reducing valve and generator. I want to ask that how...