#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0

    Classes methods etc


    Hi,

    I'm new to programming,
    I started learning a bit of C++, but people were saying for a new programmer, i should be using something easier
    to understand like Java as C++ is more advanced, doesn't have a great garbage collector and doesn't have auto memory management.

    So after lerarning some basic stuff like variables, compiling, writing some basic programs like mini calculators etc,
    i started learning Java.

    what the HELL is with all the hoops you have to jump through!?? my god.
    Methods, Classes, Instantiations and all this. I was watching one Udemy tutorial where he was making me write this...

    Code:
    package learningjava;
    
    public class Creatingobjects {
    	
    	
    public Creatingobjects() {
    		
    		System.out.println("I am the constructor");
    		
    	}
    	
    public void messageMethod() {
    		
    		System.out.println("I am the message method");
    		System.out.println("I am also the message method");
    		System.out.println("Me too mother****der");
    		
    		}
    
    public Creatingobjects(String name){
    		
    		System.out.println("My name is "+name);
    	}
    
    public static void main(String[] args) {
    		
    		int num1, num2, num3, num4, num5;
    		num1=10;
    		num2=5;
    		num3 = 11;
    		num4 = 12; 
    		num5 = 7;
    		
    		
    		
    				
    		System.out.println("The number:"+num1+num2+num3+num4+num5);
    		
    		Creatingobjects createObj = new Creatingobjects();
    		Creatingobjects createObj1 = new Creatingobjects("Tony");
    		createObj.messageMethod();
    
    	
    	
    	}
    
    }
    Why do i have to keep writing public over and over? why do i have to keep splitting everything up into little chunks in between multiple sets of
    curly braces? is that right? i mean, in C++ you barely have to write anything, and you can just list everything off all together
    for the most part, in this you have to write Public static void main(string args[]) {} and then have to keep writing public {} public {} etc.
    am i missing something? do i really need to have so many publics and classes and objects all over the place?
    and why do i have to keep writing void? i know a Method is like int x; writing int x; declares the method, and x=1; assigns a value
    to the method int X. And there again, another piece of nonsense. Why is that called a "Method", it's no such thing it's a declaration and assignment
    is al this crap just to make it seem more complicated? im mean, surely when writing this language they could have thought
    "Ok, having to write Public static void main(string args[]{} at the start of every project is too long, having to make everything an
    object, even if the "Object" is just you saying hello to yourself in a command prompt... again, doesn't seem to be much of an object
    to me!

    Any tips guys? as im either stupid or C++ seems MUCH easier to grasp. It's the silly naming schemes for things thats confusing me
    i think. The names for the processes of everything are so arbitrary and off the mark as to what they are and do, make learning
    it so much harder as you find yourself just writing what the guy is telling you to write and taking his word for it that you need
    to write public creatingObjects again without understanding why, and being confused about the "the main method, the objects,
    classes" writing "Void" everywhere, like the fock does that mean??? and having to write "Args??" Aguments? why? when iu can replace (String myName[]{} for instance?

    ANy help would be great guys, thanks a bunch
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,279
    Rep Power
    9645
    You don't have to do all that.

    Remember that there are tons of tutorials and stuff out there. You don't have to follow the first one you find. If you don't like the one you're using now then look around for one by someone who explains everything in a way that makes sense to you.

    0. Methods and variables. You say "a method is like int x;" - that's not correct. A method is a function that belongs to a class; messageMethod() and main() are both methods on the Creatingobjects class. The "x" you used is a variable, and the num1-5 and createObj/createObj1 things in main() are also variables. Classes can have variables (Java calls them "fields") too but Creatingobjects isn't using any.

    1. "public" indicates visibility - what code can see the thing. When it's public, everybody can access the value or call the method. If you don't use public then the thing is what's called "package private" where the class and other classes in the package can see it, but others cannot. There is also protected and private and they have their own meanings.

    For now let's just say that you need to put "public" on everything. Later on you'll learn more about visibility and what is appropriate to use and when.

    2. Splitting everything up, that's... well, just how the author did it in that code. It's a good idea to (try to) limit each method to one single purpose but it isn't like Java forces you to do it.

    As for why there are so many:
    - Creatingobjects() is a constructor with no arguments.
    - Creatingobjects(String) is a constructor with one String argument. It demonstrates that you're allowed to have multiple methods (or constructors) with the same name as long as their argument lists are different.
    - messageMethod() is a regular method that does something.
    - main() is the main method. Like C++'s main, this is what gets called first when your program runs.

    Each one of those has a specific purpose.

    If you did more C++ then you'd see more of this "splitting everything up" business. In fact that's how it is in all programming languages (even if some don't use {}s). If you translated this code into C++ then it would look almost the same.

    3. So many classes and objects? Kinda. Java is a very object-oriented language, but that's not necessarily a bad thing. Now, what this author did with Creatingobjects as a class isn't a good example: think of classes like nouns and class methods like verbs. If I were writing this same sort of example then I would do something more like
    java Code:
     
    package learningjava;
     
    public class Example {
     
    	public Example() {
    		System.out.println("This constructor does not have any arguments.");
    	}
     
    	public Example(String name) {
    		System.out.println("This constructor takes one String argument and its value is " + name + ".");
    	}
     
    	public void message() {
    		System.out.println("This is a regular method. It has no arguments and does not return a value.");
    	}
     
    	public static void main(String[] args) {
    		System.out.println("This is the main() method. When your program runs Java will start with it.");
     
    		System.out.println("I am about to create a new Example using the default constructor:");
    		Example e1 = new Example();
     
    		System.out.println("I am about to create a new Example using the other constructor:");
    		Example e2 = new Example("Tony");
     
    		System.out.println("I now have two Example objects: e1 and e2. I will call the message() method on e1:");
    		e1.message();
     
    		System.out.println("This is the end of main(). The program will end.");
    	}
     
    }


    There isn't really a particularly good "noun" for this class so I chose to call it Example.

    4. When you write a regular method you need to indicate what it returns. If your method doesn't return anything then you say it "returns" void.

    5. C++ may be easier to grasp but there is a lot going on that you may not see that you need to know about. Memory management, as you said, is one example: once you start using pointers you have to worry about making sure you clean up after you use them, and if you don't then you will have problems and it won't be obvious why. Java is a much safer language in that a lot of the dirty work - not just with memory - is done for you behind the scenes.

    Personally, I wouldn't recommend Java as a starting point to programming because it's too easy to learn how to use the language without learning how to use it effectively. Instead I generally recommend Python. Once you understand programming in general you can worry about more advanced topics like classes (also known as object-oriented programming).


    No offense but it will be easier to answer your questions if you can take a deep breath, eat a snack, watch some TV, and come back with a clear head and something that's a little less... rambling. I don't know exactly what tutorial you're following but it seems like they have you diving into the deep end before you've learned how to swim. Learning programming will be a much better experience if you aren't rushing into it.

    Comments on this post

    • hexman agrees : great answer! +1 for python recommendation :)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0
    ok brother, thank you SO much for this reply. Normally I'd kinda be offended as i was wrong about the variables etc. But i'm going through this post bit by bit
    whilst im also following these UDEMY course video's i paid 280 euro for. You're actually making alot more sense then how this guy is showing me, but his
    showing me actually doing it in action, with your "better" and "Easier" to understand descriptions of everything is just perfect for me.

    Thank you so much brother.
    I want to spend about 3 hours per day learning. Do you know roughly how long it would take like that to be somewhat competent enough
    to get a job in a software company as a bug tester or whatever? And should i be learning another language beside Java like SQL and PHP,
    or Ruby or those other ones? Thank you again, and i hope to hear back from you soon.
    T
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0
    by the way, the UDEMY course is talking about Mobile phones using GPRS for internet connections which if
    i remember correctly was early 2000's. is what i'll be learning from this thing still gonna be somewhat relevant and transferable
    going forward onto newer platforms using mostly the same code? or has the language changed so much since then that i'd be
    pissing against the wind? thank you
  8. #5
  9. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,279
    Rep Power
    9645
    Originally Posted by Tobougg
    I want to spend about 3 hours per day learning. Do you know roughly how long it would take like that to be somewhat competent enough
    to get a job in a software company as a bug tester or whatever?
    It depends. Really, it does. For one it depends on you, how you learn a language, and (obviously) how quickly you learn it.

    For two, it depends on the job. A traditional QA person doesn't need to know much about coding - reading helps, but writing isn't a big part. Mostly you deal with testing out parts of the application, submitting bug reports to the development team, and working with them to solve the problem. Nowadays this role is falling out of favor and being replaced with SDETs.
    An SDET (software developer in testing) is little different as you're expected to be a decent developer, enough that you can write some code and tweak some parts, but the focus is still on having a testing mindset. For example, I'm a good developer but I'm not a good SDET because I just don't have the mindset and experience to do it.

    If you want to go into QA/work as an SDET then I suggest learning a language (eg, Java) to the degree that you can write code but probably aren't the best at it, but then you should learn how to be an SDET.

    Originally Posted by Tobougg
    And should i be learning another language beside Java like SQL and PHP,
    or Ruby or those other ones?
    I recommend it simply for the varied experience with programming. Different languages work in different ways, and knowing (or at least being familiar) with more than one gives you additional experience. Example: since Python is not in the C-family of languages it has a very different way of approaching code - let alone a different syntax. Same for Ruby. PHP is in the C-family but is still quite different from Java, and has its own practices and paradigms.

    SQL is a special case and sits alongside others like HTML/CSS and Javascript: they're all tangential to whatever else you may be learning. If you need to work with databases then you need to know SQL, and if you need to work with websites and web applications then you need to know HTML, CSS, and Javascript. All that in addition to Java or PHP or whatever.

    Originally Posted by Tobougg
    by the way, the UDEMY course is talking about Mobile phones using GPRS for internet connections which if
    i remember correctly was early 2000's. is what i'll be learning from this thing still gonna be somewhat relevant and transferable
    going forward onto newer platforms using mostly the same code? or has the language changed so much since then that i'd be
    pissing against the wind? thank you
    GPRS is a low-level detail that you probably don't need to worry about. It's like knowing about the inner workings of your car's engine while driving: it helps to know a little about it but really you can leave those concerns with other people.
    All that matters to you is whether the device has an internet connection, and that part works the same way regardless of whether it's using GPRS (2G and 3G) or WiMAX/LTE (4G) or wifi.

    Android and iOS use different languages for their apps, so if you're interested in mobile development then you'd probably end up learning both eventually to be able to put apps on both stores.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Location
    India
    Posts
    200
    Rep Power
    4
    Off-Topic: @Tobougg: It's good to see that you got your answer but the next-time you post, please don't use text-wrapping as it makes your post harder to read. Devshed automatically wraps your post so when you manually wrap it, it ends up looking all scattered.

    (Automatic text-wrapping in the quick-reply box isn't working properly on some browsers like Firefox and I'll try contacting the administrator about it)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Posts
    4
    Rep Power
    0
    Originally Posted by requinix
    It depends. Really, it does. For one it depends on you, how you learn a language, and (obviously) how quickly you learn it.

    For two, it depends on the job. A traditional QA person doesn't need to know much about coding - reading helps, but writing isn't a big part. Mostly you deal with testing out parts of the application, submitting bug reports to the development team, and working with them to solve the problem. Nowadays this role is falling out of favor and being replaced with SDETs.
    An SDET (software developer in testing) is little different as you're expected to be a decent developer, enough that you can write some code and tweak some parts, but the focus is still on having a testing mindset. For example, I'm a good developer but I'm not a good SDET because I just don't have the mindset and experience to do it.

    If you want to go into QA/work as an SDET then I suggest learning a language (eg, Java) to the degree that you can write code but probably aren't the best at it, but then you should learn how to be an SDET.


    I recommend it simply for the varied experience with programming. Different languages work in different ways, and knowing (or at least being familiar) with more than one gives you additional experience. Example: since Python is not in the C-family of languages it has a very different way of approaching code - let alone a different syntax. Same for Ruby. PHP is in the C-family but is still quite different from Java, and has its own practices and paradigms.

    SQL is a special case and sits alongside others like HTML/CSS and Javascript: they're all tangential to whatever else you may be learning. If you need to work with databases then you need to know SQL, and if you need to work with websites and web applications then you need to know HTML, CSS, and Javascript. All that in addition to Java or PHP or whatever.


    GPRS is a low-level detail that you probably don't need to worry about. It's like knowing about the inner workings of your car's engine while driving: it helps to know a little about it but really you can leave those concerns with other people.
    All that matters to you is whether the device has an internet connection, and that part works the same way regardless of whether it's using GPRS (2G and 3G) or WiMAX/LTE (4G) or wifi.

    Android and iOS use different languages for their apps, so if you're interested in mobile development then you'd probably end up learning both eventually to be able to put apps on both stores.
    Hey dude, thanks again so much for all the info.
    I think I'm gonna look at Java, once I've spent a couple of weeks a few hours a day learning it, and i'm familiar with the syntax and and the
    common buzzwords and things you have to add and initiate etc without making mistakes, i.e, being able to say "Ok i want to make a basic
    calculator that can do addition, subtraction, multiplication". and be confident that i can write this code fairly neatly without few mistakes
    or errors and be able to output it then. Thats where i'm aiming at the moment. kinda knowing "there needs to be a ; after this, and there needs
    to be a class here or a string here or i need []'s here" and know what im talking about, once i'm at that stage, i think then i'll
    start looking to get to the same level with C++ so i can see the differences and it won't take too long (i have time from now until well ater
    xmas) .

    Ill be able to work on a basic but confident low level with C++ and Java, thats my goal short term. Once i go back to Java only
    for the next steps such as writing actual programs using loops and objects and actual java panels/frames etc.
    Then i think i'm going to start to look at SQL and PHP, as know there are alot of jobs for people who can work with these database
    programs and the query stuff.

    Ill look into the specific type of role you're talking about, sounds interesting. I basicallu just want to get into a QA role or some
    kind of programming related area so i can keep learning from home, get much better and then have more options, and have
    experience behind me with working in a software role of some sort you know?

    It's weird but im actually excited to learn about this stuff. Like 99% or people look at code or hear about programming and coding
    and want to vomit as it reminds them of looking at pages of nonsense numbers letters and symbols, but to me, looking at pages
    of code and the instructions on how to use them and what they are give me a feeling of excitement. have you ever had that?
    or am i just gay? lol

    last question dude, i want to actually write a fairly easy program, but one thats ACTUALLY a program you know what i mean?
    I mean it's easy to output your hello worlds and "My first class" and objects and int x = 14; etc, but even once you've copied and understood
    like a bit lengthy page of that type of code all my books and my course are teaching me about, none of them are actually programs
    of any use, they're just little things you can basically type data into like age and name and stuff. they don't feel like programs.

    What i really want is to be able to write something like a simple game, that i can compile and turn it into an exe and can
    play, this would feel like "I've actually written a real program here". Would something like Chess, or one of those boards games
    or card games be too difficult?

    Thank you again, i owe you a beer. What do you work at by the way? or are you studying/
  14. #8
  15. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,279
    Rep Power
    9645
    Originally Posted by Tobougg
    It's weird but im actually excited to learn about this stuff. Like 99% or people look at code or hear about programming and coding
    and want to vomit as it reminds them of looking at pages of nonsense numbers letters and symbols, but to me, looking at pages
    of code and the instructions on how to use them and what they are give me a feeling of excitement. have you ever had that?
    or am i just gay? lol
    It's uncommon but certainly not bad. After all, people who enjoy their job will naturally perform better than those who don't.

    Originally Posted by Tobougg
    last question dude, i want to actually write a fairly easy program, but one thats ACTUALLY a program you know what i mean?
    I mean it's easy to output your hello worlds and "My first class" and objects and int x = 14; etc, but even once you've copied and understood
    like a bit lengthy page of that type of code all my books and my course are teaching me about, none of them are actually programs
    of any use, they're just little things you can basically type data into like age and name and stuff. they don't feel like programs.
    All of those count as programs - they just don't do anything that you care about. Like, if I go to a museum and visit a gallery on abstract art, I'm going to look at it and think "this isn't art, this is people throwing paint at a canvas". But the thing is, it is art, and I just don't care about it.

    Originally Posted by Tobougg
    What i really want is to be able to write something like a simple game, that i can compile and turn it into an exe and can
    play, this would feel like "I've actually written a real program here". Would something like Chess, or one of those boards games
    or card games be too difficult?
    Don't worry about the .exe yet. You can (for example) compile Java programs to a .jar and run those just fine; it's not a native application but it does act like one. If you want actual executables then I think I would suggest getting into C# and .NET, but only after you've spent enough time with Java first.

    Chess... heh. You can make a program to do that, sure: the rules are established, the graphics aren't a big deal, and it's mostly just point-and-click. The problem is if you want to make a computer player for it. That's AI and it's much more difficult than mere programming. (At least the good AIs are.) I'm sure there's detailed algorithms documented on the internet that you can borrow from, though.

    If you like card games, blackjack would be an easy one as the AI is basically just "if card total is between X and Y then do this", and those strategies are easy to come across. It's really about statistics. Others like poker are a bit more involved but also ultimately boil down to probabilities and expected payouts.

    Originally Posted by Tobougg
    What do you work at by the way? or are you studying/
    I don't actually work anywhere at the moment :P
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Location
    India
    Posts
    200
    Rep Power
    4
    Originally Posted by Tobougg
    last question dude, i want to actually write a fairly easy program, but one thats ACTUALLY a program you know what i mean?
    I mean it's easy to output your hello worlds and "My first class" and objects and int x = 14; etc, but even once you've copied and understood
    like a bit lengthy page of that type of code all my books and my course are teaching me about, none of them are actually programs
    of any use, they're just little things you can basically type data into like age and name and stuff. they don't feel like programs.

    What i really want is to be able to write something like a simple game, that i can compile and turn it into an exe and can
    play, this would feel like "I've actually written a real program here". Would something like Chess, or one of those boards games
    or card games be too difficult?

    Thank you again, i owe you a beer. What do you work at by the way? or are you studying/
    You mean the course you're following taught you to write simple programs but not how to combine them into a complete application that does something interesting. All the courses and programming books start with little bits and pieces to teach you about a single concept like variable-declaration or simple-output/input and then they slowly advance towards composing these concepts into single units, such as a methods, classes and finally packages (in order). Then they teach you how to use these components into other components and connect them together. After that they teach you about different programming paradigms and methodologies, how a single component can be different from others, how a seemingly-useless bunch of data can be made to exhibit a certain behavior, etc. Java is primarily object-oriented so they'll teach you that first.

    Just as with mathematical functions, Java methods can be composed. That means you can use one expression as part of another.

    -Allen B Downey - How to Think Like a Computer Scientist: Think Java
    Teaching you the right tools and environment is the duty of the instructor but on the other hand, learning how to use them effectively is your job.

    If your course doesn't teaching you proper program design, how to use your code as part of another and how to use the code written by others; then maybe you're following the wrong course. But bear in mind though, even the best courses will teach you the basics first as these concepts are too advanced for a raw beginner to pick-up... perhaps you're too eager. Code is cheap and can be written by almost anyone but writing good code is an altogether different thing and requires time to master.

    If you're looking for a good place to learn then you can head over to MIT's OpenCourseWare site (Java: Bitly; C/C++: Bitly) but prepare to work hard, self-learning is a difficult task.
    Last edited by hexman; December 15th, 2016 at 10:01 AM.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    2
    Rep Power
    0
    The key concept you need to get to grips with in a language like Java is object oriented programming and subtopics like classes, objects, constructors, methods, inheritance, abstraction, coupling, cohesion, modularisation, refactoring and more. if you want to be a good programmer then you need to learn about these OOP concepts.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Location
    India
    Posts
    200
    Rep Power
    4
    Originally Posted by JohnTravolta
    The key concept you need to get to grips with in a language like Java is object oriented programming and subtopics like classes, objects, constructors, methods, inheritance, abstraction, coupling, cohesion, modularisation, refactoring and more. if you want to be a good programmer then you need to learn about these OOP concepts.
    Please don't revive an old post. The OP had clearly found their solution and necroing isn't going to help anyone.

IMN logo majestic logo threadwatch logo seochat tools logo