Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Directly change java code using user input

  1. #1
    Zyper is offline Member
    Join Date
    Nov 2014
    Posts
    1
    Rep Power
    0

    Default Directly change java code using user input

    Hello, I'm quite new to java programming, but eager to learn. I was wondering how to change actual java code, using user input. For example:

    Java Code:
     int x = 0;
     // Start the program and enter 3
     // Now the code is
     int x = 3

    Actually changing the core programmed java. If this is really advanced, then please just say so; and I'll get back to it at a later point.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Directly change java code using user input

    It is difficult to know exactly what you are asking. If you just want to change the value of x from 0 to 3 based on a user entered value, then that is trivial. Check the Scanner class in the JDK documentation for that. If you want to actually replace compiled code with new code the user enters, then I do not believe that is possible. But what can be done is to prompt the user for some new class name and then have that class loaded and made available for use. That is rather advanced and requires the use of reflection and a knowledge of class loaders.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Directly change java code using user input

    Quote Originally Posted by Zyper View Post
    Actually changing the core programmed java. If this is really advanced, then please just say so; and I'll get back to it at a later point.
    There's nothing advanced about it; self modifying code was done fifty years ago and (since then) considered hacking, unreadable and extremely error prone; it was fun to do though ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Directly change java code using user input

    Quote Originally Posted by Zyper View Post
    I was wondering how to change actual java code, using user input.
    Tip: focus on actually learning useful things. This you probably won't ever need in your career. Meanwhile try explaining what you want to achieve rather than focusing on a technical solution you picked yourself; perhaps someone can enlighten you to a way to achieve it that is more reasonable.
    Norm likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Have java wait for user input
    By kkid in forum AWT / Swing
    Replies: 3
    Last Post: 02-14-2013, 06:02 PM
  2. Java User-Input bug (not continuing); NOVICE
    By Shzylo in forum New To Java
    Replies: 8
    Last Post: 12-13-2012, 05:30 AM
  3. Convert user input into code
    By phyzix5761 in forum New To Java
    Replies: 7
    Last Post: 11-22-2012, 07:45 AM
  4. Java Date input from User,
    By Shahnawaz in forum New To Java
    Replies: 22
    Last Post: 09-07-2011, 01:20 AM
  5. Replies: 0
    Last Post: 02-21-2010, 11:51 AM

Tags for this Thread

Posting Permissions

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